由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Programming版 - a simple design question
相关主题
一个inheritance 的问题【讨论】问一道很简单的C++题。。。。 (转载)
C++面试题C++: friend function
Overridden function will cause function shadow in C++, but not in Javafriend function 不能virtual 怎么搞呢?
问个词汇问题-算法时间复杂度咋说请教一个class design的问题
Polymorphism是怎么定义的?why use static function here?
c++越写越没有信心,觉得自己水平弱的不行Java banned operator overloading
以下两个C 代码是不是完全等价?问个overloading new operator的问题
两个我永远都不想碰的语言请问一个implicit conversion的问题(C++)
相关话题的讨论汇总
话题: a1话题: b1话题: a2话题: fooa话题: b2
进入Programming版参与讨论
1 (共1页)
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

1 (共1页)
进入Programming版参与讨论
相关主题
请问一个implicit conversion的问题(C++)Polymorphism是怎么定义的?
这个问题怎么答?c++越写越没有信心,觉得自己水平弱的不行
question overloading ++ error以下两个C 代码是不是完全等价?
one question about overloading operator delete两个我永远都不想碰的语言
一个inheritance 的问题【讨论】问一道很简单的C++题。。。。 (转载)
C++面试题C++: friend function
Overridden function will cause function shadow in C++, but not in Javafriend function 不能virtual 怎么搞呢?
问个词汇问题-算法时间复杂度咋说请教一个class design的问题
相关话题的讨论汇总
话题: a1话题: b1话题: a2话题: fooa话题: b2