STL List、C++ 中的多种对象类型
所以我有这种类型的继承:
Class A { virtual intfB(){} ; virtual intfC(){}; };
Class B : public A { int fB(){}; };
Class C : public A { int fC(){}; };
然后我有一个类 E,它使用列表来存储 B 和 C 对象。
Class E
{
public:
insert(A* obj){ l.push_front(obj);}
print(){ /* uses iterator i to cout the fB() and fC() of the objects in l */ };
private:
list <A*> l;
}
我将B
和C
对象的不同函数设置为A
中的virtual
,然后使用迭代器i来访问print()
函数中的 l 对象。我的问题是我找不到调用 B::fB()
和 C::fC()
的正确方法,因为迭代器可以指向 C
或 B
对象,所以我必须对每个我认为不实用的对象执行类似的操作:
cout << (*i) -> fB();
cout << (*i) -> fC();
所以基本上我都调用 fC
和fB
对于每个对象,无论类型和当调用的对象不正确时,将调用基类的虚函数。
但这不可能是正确的,是吗?我还能怎样实现这一目标?
顺便说一句,我应该对 E、C 和 B 类使用组合吗?
So i have this type of inheritance:
Class A { virtual intfB(){} ; virtual intfC(){}; };
Class B : public A { int fB(){}; };
Class C : public A { int fC(){}; };
Then i have a class E that uses a list to store B and C objects.
Class E
{
public:
insert(A* obj){ l.push_front(obj);}
print(){ /* uses iterator i to cout the fB() and fC() of the objects in l */ };
private:
list <A*> l;
}
I set different functions of the B
and C
objects as virtual
in A
and then use an iterator i to access the l objects from within the print()
function. My problem is that i cannot find a proper way to call B::fB()
and C::fC()
because the iterator can point to either a C
or B
object so i have to do something like this for each object that i don' t think is that practical:
cout << (*i) -> fB();
cout << (*i) -> fC();
So basicaly i call both fC
and fB
for every object regardless of type and the virtual function of the base class is called when it is not the correct object called.
This cannot be right tho, is it? How else can i achieve that?
On a side note should i use composition for classes E, C and B?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你是对的:不要那样做。如果要打印某个项目,请调用
A
中的函数print
,并在两个子类中重写它。然后,您在迭代时对每个项目调用print
,并且每个子项都会执行正确的操作。You're correct: Don't do it that way. If you want to print an item, call the function
print
inA
, and override it in both child classes. Then you callprint
on each item as you iterate and each child does the right thing.如果 intfB() 和 intfC() 在技术上是相同的函数,但 B 和 C 的实现不同,那么您应该只在 A 中声明一个虚拟函数(可能称为 intf),并在 B 和 C 中为其提供必要的实现 抽象类
的想法是定义该类的更详细实现将共享的基本功能。因此,通过上述方案,列表元素指向的每个对象都将保证具有 intf() 实现。
我不确定你的示例是否会编译(不过我还没有测试过),因为 B 和 C 都没有提供 A 中声明的虚拟函数的实现。
If intfB() and intfC() are technically the same function but with different implementations for B and C, then you should just declare one virtual function in A (perhaps called intf), and provide the necessary implementations for it in both B and C.
The idea of an abstract class is to define the base functionality that more detailed implementations of that class will share. So with the above described scheme, every object pointed to by the elements of your list will be guaranteed to have a intf() implementation.
I'm not sure that your example will compile (I have not tested that though) because neither B nor C provide implementations for the virtual functions declared in A.