I’m trying to figure out the most logical, safe, efficient way to accomplish this, without using global variables.
Suppose I have a class:
class SomeClass{
public: someFunction(){...}
};
And another class instantiates SomeClass as a data member:
class AnotherClass{
SomeClass theTest;
void anotherFunction(){...}
int myDataInt;
};
Now, I want the someFunction in SomeClass to be able to easily access the myDataInt in AnotherClass. One way would be to add an int parameter to someFunction that AnotherClass passes in. Perhaps that is the only decent way to do this. But I was hoping for a more “automatic” access between someFunction and myDataInt. Another option is to simply have AnotherClass set data in SomeClass that is equal to its own myDataInt. But then I have two classes storing the same data, which seems redundant.
Any other suggestions? Much appreciated.
Add a pointer to the containing
AnotherClassinSomeClass:EDIT: Also, as mentioned by iammilind above, you will want to either have
SomeClassbe a friend ofAnotherClassor have an accessor method formyDataInt.