aa is a pointer so you have to either dynamically allocate an instance of a, or set it to the address of an already instantiated instance, and call aa->getAttribute(); . Alternatively do
aa a;
a.getAttribute();
twomers
Posting Virtuoso
1,877 posts since May 2007
Reputation Points: 453
Solved Threads: 57
Nope.
class thing {
private:
std::string str;
public:
thing(std::string _str)
: str(_str) {
}
std::string &getAttribute() {
return str;
}
};
int main(int argc, char* argv[]) {
thing inst("one");
std::cout<< inst.getAttribute() << "\n";
inst.getAttribute() = "two";
std::cout<< inst.getAttribute() << "\n";
thing *ptr = &inst;
ptr->getAttribute() = "three";
std::cout<< inst.getAttribute() << "\n";
return 0;
}
Because getAttribute() returns the address of str if means you can assign variables to it using the = operator, as above.
twomers
Posting Virtuoso
1,877 posts since May 2007
Reputation Points: 453
Solved Threads: 57