I’m trying to access the method a() in Foo by creating a new object from Foo and calling its duplicate method (duplicate creates a new Foo object). Then I call ::a() since I should have access to the class. But it’s not working. Can anyone explain why?
#include <iostream>
using std::cout;
class Foo {
public:
int a() { return 5; }
Foo *duplicate() {
return new Foo();
}
};
int main() {
Foo foo;
Foo *a = foo.duplicate()::a(); // should return 5
cout << a;
}
You must use the
->operator to access members of object pointers. So try this:And you cant assign 5 (an integer as returned by the
Foo::a()method) toFoo *a, why are you trying to do that?