Dear All,
many thanks for you replies.
Again forgive me for stating the previous problems in a too simplified form.
The actual, I think ultimate, problem is the following:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
|
#include <iostream>
using namespace std;
class A
{
public:
virtual int foo(int i) = 0;
virtual void bar() = 0;
};
class B: public A
{
public:
B(int i)
{
ID = i;
}
int foo(int i)
{
cout << "B::foo: " << this->ID << endl;
return 0;
}
void bar()
{
cout << "B::bar" << endl;
}
private:
int ID;
};
class C
{
public:
C(int i)
{
ID = i;
}
virtual double foo(int i)
{
cout << "C::foo" << endl;
return 0.0;
}
int ID;
};
class preD: public A
{
public:
int foo(int i)
{
cout << "preD::foo: " << "_I need to access C::ID_" << endl;
return 0;
}
};
class D: public preD, public C
{
public:
D(int i) : C(i) {};
// int foo(int i)
// {
// cout << "D::foo" << this->C::ID << endl;
// return 0;
// }
void bar()
{
cout << "D::bar: " << this->C::ID << endl;
};
};
int main ()
{
A* a1 = new B(1);
A* a2 = new D(2);
a1->foo(0);
a1->bar();
a2->foo(0);
a2->bar();
return 0;
}
| |
As you can see your solution is totally fine if class preD doesn't need to access anything from class C, but my problem is that preD::foo needs to access something initialized in class C.
I can't modify class A, nor class B, nor class C. Only D, preD, and the main program.
I need to be able in the main program to instantiate the class A as new B or new D, or preD, and use either B::foo or D::foo (or preD::foo)
Many thanks for your help.
Regards,
- Mauro.