cplusplus
.com
TUTORIALS
REFERENCE
ARTICLES
FORUM
C++
Tutorials
Reference
Articles
Forum
Forum
Beginners
Windows Programming
UNIX/Linux Programming
General C++ Programming
Lounge
Jobs
Forum
General C++ Programming
Using a class method to assign a value t
Using a class method to assign a value to a private member.
Jun 1, 2018 at 11:54pm UTC
Non Sequitur
(7)
Folks, I am trying to use a method within a class to assign a value to a private member of that class. I can't figure this out to save my hind-parts. Can anyone please point ou tthe obvious?
#include <cstdlib>
#include <cstdio>
using namespace std;
class NoClass {
public:
NoClass() { };
NoClass(const NoClass& orig) { };
virtual ~NoClass() { };
void SetAnInt() {
this->anInt = ???;
}
int GetAnInt() {
return this->anInt;
}
private:
int anInt;
};
int main(int argc, char** argv) {
NoClass *nc = new NoClass();
nc->SetAnInt() = 133; // Important part here
printf("%d\n", nc->GetAnInt());
return 0;
}
Jun 2, 2018 at 1:13am UTC
jonnin
(11438)
class foo
{
private:
int x;
public:
void setx(int val) {x = val;}
}
foo f;
f.setx(3);
Topic archived. No new replies allowed.