2015-09-07 26 views
-3

我有一个基类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()? 在此先感谢。

+0

顺便说一下,你的'Clone'只是返回'this'而不是'* this'的副本。 – Jarod42

回答

1

它不无dynamic_cast工作,因为Animal::clone静态返回类型为Animal*动态类型animal6Specie<Dog>,但是它没有输入在编译时推导出的函数返回clone函数的类型。

如果你真的需要这个,dynamic_cast是必要的,但这是一种代码味道。如果你发现自己需要绝对动态类型的对象,而不仅仅是使用虚拟功能,那么你应该考虑重新设计。

+0

谢谢。我在函数clone()中使用dynamic_cast,但它似乎没有帮助。 –

+0

@ ChristopheJ.Ortiz不会改变'clone'函数的静态类型。 – TartanLlama