Let me see if I'm understanding you correctly. You have two classes, each with a pointer to an array of X's.
1 2 3 4
|
class X;
class A{ X* ptr; };
class B{ X* ptr; };
|
And the pointers in A and B are likely to point to the same array, or part of the same array.
And your goal is, if a pointer in a B object is assigned a new memory address, to be able to reflect the change in the A object as well?
Well, there are several options:
-Use std::shared_ptr (or boost's). This ensures that the memory will be cleaned up at some point. Then you can change the pointers at A and B objects separately.
-Use X** as you mentioned, so the two objects will always use the same pointer.
-Create a function in both A and B that lets you place requests to change the pointers, then allow that function to do the clean up. Of course, using a function means you will probably need a second function to pass the object of the other class as well.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
|
void A::change_ptr(X* newptr){
delete[] this->ptr;
this->ptr = newptr;
}
void A::change_ptr(X* newptr, B& twin){
this->ptr = newptr;
twin.change_ptr(newptr);
}
void B::change_ptr(X* newptr){
delete[] this->ptr;
this->ptr = newptr;
}
void B::change_ptr(X* newptr, A& twin){
this->ptr = newptr;
twin.change_ptr(newptr);
}
|