多重继承的模糊解决方法?
Ambiguous workaround for multiinheritance?
我有一个名为 animal 的基 class,以及从 Animal 继承的一只狗和一只猫。
还有一个名为 dogcat 的多重继承 class,它继承自 dog 和 cat,
在 Animal 中,我有一种叫做睡眠的方法。当我想使用 dogcat 的那个方法时,我得到错误 "DogCat::sleep" is ambiguous,我确实理解这个问题,但我在一本书中读到它应该是可能的,当你声明 sleep 为虚拟时 - 但它确实如此不行。
这不可能是书错了还是有什么解决方法?
class Animal
{
public:
Animal(){}
virtual void sleep()
{
cout << "zzzzzzzzz" << endl;
}
virtual void eat() = 0;
};
class Dog: public Animal
{
protected:
Dog(){}
virtual void eat() override
{
cout << "eats dogfood" << endl;
}
};
class Cat :public Animal
{
public:
Cat(){}
virtual void eat() override
{
cout << "eats catfood" << endl;
}
};
class DogCat : public Dog, public Cat
{
public:
DogCat(){}
using Dog::eat;
};
int main(int argc, char** argv) {
DogCat *DC = new DogCat();
DC->sleep();//Error
}
你应该使用虚拟继承
class Animal
{
public:
Animal(){}
virtual void sleep()
{
cout << "zzzzzzzzz" << endl;
}
virtual void eat() = 0;
};
class Dog: virtual public Animal
{
protected:
Dog(){}
virtual void eat() override
{
cout << "eats dogfood" << endl;
}
};
class Cat : virtual public Animal
{
public:
Cat(){}
virtual void eat() override
{
cout << "eats catfood" << endl;
}
};
class DogCat : public Dog, public Cat
{
public:
DogCat(){}
using Dog::eat;
};
int main(int argc, char** argv) {
DogCat *DC = new DogCat();
DC->sleep();//Error
}
The "diamond problem" (sometimes referred to as the "deadly diamond of death"[4]) is an ambiguity that arises when two classes B and C inherit from A, and class D inherits from both B and C. If there is a method in A that B and C have overridden, and D does not override it, then which version of the method does D inherit: that of B, or that of C?
所以。现在你有两个 A 实例。解决方案是什么?你有两个:
- 在其中一个子类中定义睡眠操作并调用它:
class Cat :public Animal
{
public:
Cat(){}
virtual void eat() override
{
cout << "eats catfood" << endl;
}
void sleep()
{
Animal::sleep();
}
};
int main(int argc, char** argv) {
DogCat *DC = new DogCat();
DC->Cat::sleep();
}
- 像@Asesh 回答的那样使用虚拟继承。问题出在常用方法eat()。你必须覆盖它。
我有一个名为 animal 的基 class,以及从 Animal 继承的一只狗和一只猫。 还有一个名为 dogcat 的多重继承 class,它继承自 dog 和 cat, 在 Animal 中,我有一种叫做睡眠的方法。当我想使用 dogcat 的那个方法时,我得到错误 "DogCat::sleep" is ambiguous,我确实理解这个问题,但我在一本书中读到它应该是可能的,当你声明 sleep 为虚拟时 - 但它确实如此不行。
这不可能是书错了还是有什么解决方法?
class Animal
{
public:
Animal(){}
virtual void sleep()
{
cout << "zzzzzzzzz" << endl;
}
virtual void eat() = 0;
};
class Dog: public Animal
{
protected:
Dog(){}
virtual void eat() override
{
cout << "eats dogfood" << endl;
}
};
class Cat :public Animal
{
public:
Cat(){}
virtual void eat() override
{
cout << "eats catfood" << endl;
}
};
class DogCat : public Dog, public Cat
{
public:
DogCat(){}
using Dog::eat;
};
int main(int argc, char** argv) {
DogCat *DC = new DogCat();
DC->sleep();//Error
}
你应该使用虚拟继承
class Animal
{
public:
Animal(){}
virtual void sleep()
{
cout << "zzzzzzzzz" << endl;
}
virtual void eat() = 0;
};
class Dog: virtual public Animal
{
protected:
Dog(){}
virtual void eat() override
{
cout << "eats dogfood" << endl;
}
};
class Cat : virtual public Animal
{
public:
Cat(){}
virtual void eat() override
{
cout << "eats catfood" << endl;
}
};
class DogCat : public Dog, public Cat
{
public:
DogCat(){}
using Dog::eat;
};
int main(int argc, char** argv) {
DogCat *DC = new DogCat();
DC->sleep();//Error
}
The "diamond problem" (sometimes referred to as the "deadly diamond of death"[4]) is an ambiguity that arises when two classes B and C inherit from A, and class D inherits from both B and C. If there is a method in A that B and C have overridden, and D does not override it, then which version of the method does D inherit: that of B, or that of C?
所以。现在你有两个 A 实例。解决方案是什么?你有两个:
- 在其中一个子类中定义睡眠操作并调用它:
class Cat :public Animal { public: Cat(){} virtual void eat() override { cout << "eats catfood" << endl; } void sleep() { Animal::sleep(); } }; int main(int argc, char** argv) { DogCat *DC = new DogCat(); DC->Cat::sleep(); }
- 像@Asesh 回答的那样使用虚拟继承。问题出在常用方法eat()。你必须覆盖它。