C++中的多态性永远不会升级
Polymorphism in C++ never upcasts
我做java开发人员已经很长时间了。我肯定我想错了。
但是我有一个等级动物和一个等级狗。狗延伸了动物,在我的主要,我做了一堆狗,并把它们粘在动物的载体上。
在对它们进行迭代时,我无法访问最初包含Dog的对象上的Dog特定方法。
我做错了什么?
动物
#include "Header.h"
class Animal {
std::string name;
int age;
public:
std::string getName(){ return name;}
virtual int getAge(){ return age;}
virtual int getRealAge();
Animal(std::string name, int age);
~Animal();
};
Animal::Animal(std::string name, int age){
this->name = name;
this->age = age;
}
int Animal::getRealAge() {
return this->age*100/2;
}
Animal::~Animal() {
}
狗
#include "Header.h"
class Dog : public Animal {
public :
Dog(std::string name, int age);
~Dog();
int getAge();
};
Dog::Dog(std::string name, int age) : Animal::Animal(name, age) {
}
Dog::~Dog(){
}
int Dog::getAge() {
return Animal::getAge()*7;
}
主
#include "Header.h"
using namespace std;
int main() {
cout << "Hello, World!" << endl;
// I make a bunch of Dogs
Dog tmp0("Ellie", 1);
Dog tmp5("sam", 2);
Dog tmp4("lindsay", 3);
Dog tmp3("shilo", 4);
Dog tmp2("rex", 5);
Dog tmp1("bob", 6);
Animal tmp00("nick", 11);
vector<Animal*> animals;
animals.push_back(&tmp0); // Dog is-an Animal
animals.push_back(&tmp1);
animals.push_back(&tmp2);
animals.push_back(&tmp3);
animals.push_back(&tmp4);
animals.push_back(&tmp5);
animals.push_back(&tmp00); //new
for(auto &animal : animals){
Animal *tmp = NULL;
if (typeid(animal) == typeid(Animal)){
cout << "Found animal" << endl;
}else{
cout << "Found animal" << endl;
cout << animal->getName() << " is " << animal->getAge() << endl;
}
}
return 0;
};
输出
Found Dog
Ellie is 1
Found Dog
bob is 6
Found Dog
rex is 5
Found Dog
shilo is 4
Found Dog
lindsay is 3
Found Dog
sam is 2
Found Dog
nick is 11
但它们都应该乘以7。
编辑
我将虚拟类型添加到上面Animal类中的方法中,并将Animal类型的Object添加到我的向量中。
但是,在main中的for循环中,对象也以dog类型返回。
您需要使getAge
成为一个虚拟函数。
http://en.wikipedia.org/wiki/Virtual_function
我不确定你说的单词in the for loop in the main, the object returns as type dog as well
是什么意思。如果你的意思是,typeid(animal) == typeid(Animal)
的条件永远不成立,那么它应该如何工作,因为typeid将返回大多数派生的动态对象类型。
我建议您编写一个多态函数,接受最合适的对象,而不是typeid
。也就是说,你可以写
void animal_test(Dog& dog)
{
/* this will be called when argument is Dog or its ancestor… */
}
void animal_test(Animal& any)
{
/* this will be called in all other cases */
}
至于虚拟函数,这是Java和C++的主要区别,因为在C++中,默认情况下函数不是虚拟的。如果函数不是虚拟的(至少在类层次结构中的某个点上,比用于调用该函数的对象指针更高),那么将调用哪个函数完全取决于指针类型。
将getAge虚拟化是明智的选择,但至少值得注意的是,您可以通过替换。。。
if (typeid(animal) == typeid(Animal)){
cout << "Found animal" << endl;
}else{
cout << "Found animal" << endl;
cout << animal->getName() << " is " << animal->getAge() << endl;
}
具有
if (Dog* p = dynamic_cast<Dog*>(&animal))
{
// inside here, p is known to be a Dog* at compile time, so
// getAge() is statically dispatched to Dog::getAge
cout << "Found animal/dog" << endl;
cout << p->getName() << " is " << p->getAge() << endl;
}else{
cout << "Found animal" << endl;
}
这个dynamic_cast<>
在C++中被认为有点反模式(每个使用getAge()
的客户端代码站点都必须知道派生类的细节,这太疯狂了),virtual
函数是更好的方法。
- 多态性和功能结合
- 找不到成员对象:没有名为get_event()的成员,也处理多态性和向量
- 使用取消引用的指针的多态性会产生意外的结果.为什么?
- C++boost序列化多态性问题
- 如何查找哪个类对象位于数组的特定索引上(多态性)
- 如何在多线程中正确使用unique_ptr进行多态性?
- 具有智能指针的多态性
- 在 C++ 中在堆栈上创建实例时如何保持多态性?
- 继承/多态性 - 我是否被迫使用"protected"变量?
- C++ 多态性在代码::块 17.12 中不起作用
- C++ 泛型和多态性:这种模式可行吗?
- 为什么我们实际上需要运行时多态性?
- 如何在这个简单的最小示例中实现多态性?
- 如何使用静态多态性在 int 和指针类型之间进行转换?
- 无法初始化已知大小的矢量指针,该大小不会因多态性而更改
- 运行时多态性和dynamic_cast需要澄清
- 如何调用指针类型的方法(禁用多态性)?
- 从基类调用函数的多态性
- 运行时多态性 - 箭头运算符访问了错误的成员?
- C++中的多态性永远不会升级