class A {
private:
B* myPointer;
public:
void someMethod() {
B b = B();
b.setPointer(myPointer);
}
};
class B {
public:
void setPointer(B* pointer) {
pointer = this;
// the pointer in A should now be set but isnt
}
};
int main(int argc, char* argv) {
A a = A();
a.someMethod();
}
Why does myPointer not get set? I want it pointing to b. It's important that myPointer gets set in a function of class A.
At line 17, you're passing pointer by value. Any changes to pointer won't be reflected in the caller. It also doesn't make much sense since the caller could accomplish the same thing by simply taking the address of b without needing to call setpointer.
Lines 8 and 25 don't make sense. It looks like you're trying to set b to the default constructor of B. That's not necessary, since B's default constructor will get called anyway.
It's important that myPointer gets set in a function of class A
I don't see anything that attempts to do that.
I read your other thread, and I still don't understand what you're trying to do.
A has no default constructor, so A::mypointer is uninitialized.