虚方法不调用派生类方法. -C ++ [英] Virtual Method not calling dervied class method. - C++

查看:94
本文介绍了虚方法不调用派生类方法. -C ++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在基类和派生类中都有一个方法.当我在派生类中的对象上调用此方法时,它将调用基类方法,而不是派生类方法.这是我的代码:

Person.h

class Person
{
...
public:
...
    virtual void coutPerson();
};

Person.cpp

void Person::coutPerson() {
    cout << name << endl;
    birthday.coutDate();
    phoneNumber.coutPhoneNumber();
    cout << city << ", " << state << endl;
}

Student.h

class Student : public Person
{
...
public:
...
    virtual void coutPerson();
};

Student.cpp

void Student::coutPerson() {
    cout << "DEBUG: Student::coutPerson()" << endl;
    //Person::coutPerson();
    cout << "Dorm Room: " << this->dorm << " " << this->dormRoom << endl;
}

在以下位置创建的对象:addPerson<Student>(personVector); 创建对象时,将其作为学生创建,因为它会调用学生构造函数. 在personVector[num-1].coutPerson();处调用的方法 Student对象是被告知给coutPerson的对象,因为它显示的是我创建Student对象时提供的信息.

template<typename T> void addPerson(vector<Person> &personVector) {
    T p;
    personVector.push_back(p);
}

在Student对象上调用coutPerson()方法时,它仅列出名称,生日,phoneNumber和城市&状态.此代码有什么问题?它应该调用Student coutMethod ...

谢谢!

解决方案

您的personVector包含Person个对象.不是引用Person对象,而是实际的Person对象.当您将您的Student对象放入向量中"时,真正发生的是该向量构造了一个 new Person对象,并使用您赋予的Student对象将其复制初始化.它,即,它创建Student对象的Person部分的副本.这种行为也称为切片".

由于向量中没有Student对象,只有一个Person对象,因此在其上调用coutPerson会调用Person::coutPerson而不是Student::coutPerson也就不足为奇了.

如果您想要多态行为,则必须在容器中存储指针(最好是智能指针,例如shared_ptrunique_ptr).但是请注意,在函数中构造的对象p在返回时会被破坏,因此为了使该对象能够生存(因此您可以拥有指向它的指针),必须使用new对其进行分配.

I have a method in both the base class and the derived class. When I call this method on an object in the derived class it calls the base class method and not the derived class method. Here is my code:

Person.h

class Person
{
...
public:
...
    virtual void coutPerson();
};

Person.cpp

void Person::coutPerson() {
    cout << name << endl;
    birthday.coutDate();
    phoneNumber.coutPhoneNumber();
    cout << city << ", " << state << endl;
}

Student.h

class Student : public Person
{
...
public:
...
    virtual void coutPerson();
};

Student.cpp

void Student::coutPerson() {
    cout << "DEBUG: Student::coutPerson()" << endl;
    //Person::coutPerson();
    cout << "Dorm Room: " << this->dorm << " " << this->dormRoom << endl;
}

Object created at: addPerson<Student>(personVector); When object is created it is created as a Student because it calls the Student construtor. Method called at: personVector[num-1].coutPerson(); The Student object is the one being told to coutPerson because it is displaying the info that I gave it when I created the Student object.

template<typename T> void addPerson(vector<Person> &personVector) {
    T p;
    personVector.push_back(p);
}

When the coutPerson() method is called on a Student object it only couts the name, birthday, phoneNumber, and city & state. What is wrong with this code? It should be calling the Student coutMethod...

Thanks!

解决方案

Your personVector contains Person objects. Not references to Person objects, but actual Person objects. When you "put your Student object into the vector", what really happens is that the vector constructs a new Person object, which is copy-initialized with the Student object you give to it, that is, it creates a copy of the Person part of the Student object. This behaviour is also known as "slicing".

Since there is no Student object in the vector, but only a Person object, it is no surprise that calling coutPerson on it calls Person::coutPerson, not Student::coutPerson.

If you want polymorphic behaviour, you have to store pointers (preferably smart pointers like shared_ptr or unique_ptr) in the container. Note however that the object p you constructed in your function is destroyed on return, so for the object to survive (so you can have a pointer to it) you have to allocate it with new.

这篇关于虚方法不调用派生类方法. -C ++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆