Obviously, this is a simplified example. One solution is to nest class B within class A. However, in my actual code, class A has a vector of class B. I just tested it, that would work fine, but is there a better way to solve this?
Example code
Expand|Select|Wrap|Line Numbers
- #include <iostream.h>
- #include <stdlib.h>
- class B
- {
- public:
- void Recurse()
- {
- this->Print(); // want a.Print(), not b.Print()
- }
- void Print()
- {
- cout << "B" << endl;
- }
- };
- class A
- {
- public:
- B b;
- void Recurse()
- {
- b.Recurse();
- }
- void Print()
- {
- cout << "A" << endl;
- }
- };
- int main()
- {
- A a = A();
- a.Recurse();
- system("PAUSE");
- return 0;
- }