如果一个类与另一个类有“has-a”关系,并且它派生了同一个类,会发生什么情况?
class A
{
friend classB;
// here lots of things might be , but i just try to understand how should I think.
};
class B : public A
{
// here again might be couple lines of codes.
protected:
A d;
};
这里究竟发生了什么?我应该如何考虑?
最佳答案
这没什么特别的。您有一个名为 d
的 class A
成员变量,并且您的 class B
还包含 class A
包含的所有内容,以及任何额外添加的内容。
我真的不能一眼看出它有多大用处,但我确信在某些情况下它可能会派上用场 - 拥有一个指向 A 对象的指针会更有意义,因为它会使它链表(或类似的)。
关于c++ - 派生自并合成相同的类。 C++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16416009/