我有一个基类Animal和模板派生类Specie<T>
。当我有一个指向基类Animal的指针时,我想使用多态克隆来返回右派生类型的对象。我实现的克隆似乎总是返回一个指向Animal的指针。不是我所期望的,但我不明白为什么。另一方面,如果我手动执行dynamic_cast,它就会起作用。任何想法 ?C++意外的多态克隆行为
#include <iostream>
class Dog;
class Cat;
class Rat;
// Base class
class Animal{
public:
virtual Animal * clone() = 0;
};
// Derived class
template <class T1>
class Specie: public Animal{
public:
Specie<T1> * clone();
void DoSomething();
};
// Purpose of clone(): Return a pointer of type Specie<T1> when applied
// to a pointer to class Animal
template <class T1>
Specie<T1> * Specie<T1>::clone(){
Specie<T1> *obj;
obj = dynamic_cast<Specie<T1> *>(this);
return obj;
}
// To identify a Dog
template <>
void Specie<Dog>::DoSomething(){
std::cout << "This is a Dog..." << std::endl;
}
// To identify a Cat
template <>
void Specie<Cat>::DoSomething(){
std::cout << "This is a Cat..." << std::endl;
}
int main(){
Specie<Dog> Dingo;
Specie<Cat> Tom;
Dingo.DoSomething();
Tom.DoSomething();
Animal *animal3;
animal3 = &Dingo;
// The following works
// Successfull conversion from pointer-to-Animal to pointer-to-Specie<Cat> with dynamic_cast without using clone()
Animal *animal4 = new Specie<Cat>;
Specie<Cat> *animal5;
// Here I can convert the pointer to Animal to a pointer to Specie<T>
// using dynamic_cast. If animal5 was not of the correct type, the compiler would return an error.
animal5 = dynamic_cast<Specie<Cat>*>(animal4);
animal5->DoSomething(); // result ok
// I want to do the same in an automated manner with clone()
// The following DOES NOT WORK with clone()
// clone() does not return a pointer to Specie<T> as expected
// but a pointer to Animal. The compiler complains.
Animal *animal6 = new Specie<Dog>;
Specie<Dog> *bobby;
bobby = animal6->clone();
return 0;
}
错误:“*动物”不能被分配给类型的实体“正金*” 巴比= animal6->克隆()类型的值;
它为什么在main中使用dynamic_cast但不使用clone()? 在此先感谢。
顺便说一下,你的'Clone'只是返回'this'而不是'* this'的副本。 – Jarod42