C ++多态克隆的意外行为

时间:2015-09-07 14:46:14

标签: c++ polymorphism clone

我有一个基类Animal和模板派生类Specie<T>。当我有一个指向基类Animal的指针时,我想使用多态克隆来返回右派生类型的对象。我实现的克隆似乎总是返回一个指向Animal的指针。不是我的预期,但我不明白为什么。另一方面,如果我“手动”进行dynamic_cast,它就可以了。有什么想法吗?

#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;
}

错误:无法将“Animal *”类型的值分配给“Specie *”类型的实体   bobby = animal6-&gt; clone();

为什么它在main中使用dynamic_cast但不使用clone()? 提前谢谢。

1 个答案:

答案 0 :(得分:1)

没有dynamic_cast,它无法正常工作,因为Animal::clone静态返回类型为Animal*animal6动态类型为Specie<Dog>,但这并不会输入在编译时为{{1}的返回而推断的类型功能。

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

相关问题