sc 发帖数: 122 | 1 Sorry for the naive question, but I couldn't figure out a better solution.
In C++, I have A1, A2 derived from A,
B1, B2 derived from B.
A is abstract, has pure virtual fooA(), which gets overloaded later. A1, A2
have their fooA1(), fooA2() respectively.
B "has-a" A. B::fooB() will be calling A::fooA(). At the same time, B1 and
B2 will need to call A1::fooA1() and A2::fooA2() as well.
Option 1:
If I make A as a data member of base B, and use polymorphism to instantiate
A with A1 or A2 (in B1 or B2), I have to cast A to A1 or A2 if I need to
call fooA1() or fooA2().
class B {
public:
A* m_A;
...
fooB(){ m_A->fooA(); }
}
B1::B1():m_A(new A1){ cast->fooA1(); // ugly }
Option 2:
If I make A as data member of B1 or B2, I have to:
class B {
public:
virtual A* getA() =0;
fooB() { getA()->fooA(); // ugly }
}
Giving A1* as a data member for B1 (and overloading getA()) is also ugly,
but this avoids casting.
How to resolve this? Any elegant solution?
Thanks | b*******s 发帖数: 5216 | 2 try
using A1::fooA;
in a sub-class of A1 for calling instanceA1.fooA() | X****r 发帖数: 3557 | 3 You can keep both an A* instance in B and an A1* instance in B1
that point to the same A1 object. After all, B1 and B need
different interfaces for A1 and A so it is fair to keep two
pointers even they happen to point to the same object (and you
can change that later if needed, e.g. if you need to use an
adapter or something). Even between base and derived classes,
low coupling is always desirable.
solution.
A1, A2
and
instantiate
to
【在 sc 的大作中提到】 : Sorry for the naive question, but I couldn't figure out a better solution. : In C++, I have A1, A2 derived from A, : B1, B2 derived from B. : A is abstract, has pure virtual fooA(), which gets overloaded later. A1, A2 : have their fooA1(), fooA2() respectively. : B "has-a" A. B::fooB() will be calling A::fooA(). At the same time, B1 and : B2 will need to call A1::fooA1() and A2::fooA2() as well. : Option 1: : If I make A as a data member of base B, and use polymorphism to instantiate : A with A1 or A2 (in B1 or B2), I have to cast A to A1 or A2 if I need to
| sc 发帖数: 122 | 4 This makes perfect sense. Thank you.
【在 X****r 的大作中提到】 : You can keep both an A* instance in B and an A1* instance in B1 : that point to the same A1 object. After all, B1 and B need : different interfaces for A1 and A so it is fair to keep two : pointers even they happen to point to the same object (and you : can change that later if needed, e.g. if you need to use an : adapter or something). Even between base and derived classes, : low coupling is always desirable. : : solution. : A1, A2
| d****p 发帖数: 685 | 5 To me, both are fine since the binding between A{i} and B{i} is at compile
time . It would be nicer if we place such binding to a higher level instead
of inside class body.
I would propose
template // T could be either A1 or A2
class DerivedB : public B
{
DerivedB(T* t) : B(t) // use the same type of T to initialize B
subobj
{
//...
}
Foo()
{
FooB();
T::FooA();
}
};
A2
instantiate
【在 sc 的大作中提到】 : Sorry for the naive question, but I couldn't figure out a better solution. : In C++, I have A1, A2 derived from A, : B1, B2 derived from B. : A is abstract, has pure virtual fooA(), which gets overloaded later. A1, A2 : have their fooA1(), fooA2() respectively. : B "has-a" A. B::fooB() will be calling A::fooA(). At the same time, B1 and : B2 will need to call A1::fooA1() and A2::fooA2() as well. : Option 1: : If I make A as a data member of base B, and use polymorphism to instantiate : A with A1 or A2 (in B1 or B2), I have to cast A to A1 or A2 if I need to
|
|