C ++覆盖的虚函数未调用 [英] C++ overridden virtual function not getting called

查看:144
本文介绍了C ++覆盖的虚函数未调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下设置:

main.cpp:

int main()
{
    vector <Tour> tourList;
    Tour* tour_ptr;

    for (unsigned int i = 0; i < tourList.size(); i++)
    {
        tour_ptr = &tourList[i];
        tour_ptr->display();
    }
}

Tour.h:

class Tour
{
   public:
    virtual void display();
};

Tour.cpp:

void Tour::display()
{
    cout << "Tour ID: " << getID() << "\n";
    cout << "Description: " << getdescription() << "\n";
    cout << "Tour Fee: $" << getfee() << "\n";
    cout << "Total Bookings: " << getbookings() << "\n\n";
}

GuidedTour.h:

GuidedTour.h:

class GuidedTour : public Tour
{
    public:
            void display();
};

GuidedTour.cpp:

GuidedTour.cpp:

void GuidedTour::display()
{
    Tour::display();
    cout << "Max Tour Group Size: " << getMaxTourists() << "\n";
    cout << "Tour Guide: " << getGuideName() << "\n";
    cout << "Tour Date: " << getTourDate() << "\n\n";
}

GuidedTour继承自Tour类,我指定了display()函数在基础Tour类中作为虚拟函数,但是由于某些原因,从来没有调用GuidedTour display()函数,而是每次都仅调用基础函数。我在做什么错?

GuidedTour inherits from the Tour class, and I've specified the display() function as virtual in the base Tour class, but for some reason, the GuidedTour display() function never gets called, only the base function gets called every time. What am I doing wrong?

推荐答案

您的代码实际上不显示任何内容,因为 std ::向量最初为空。除此之外,您的问题是由对象切片引起的(我假设您是 push_back() ing GuidedTour 放入向量中。)

Your code actually doesn't print anything as the std::vector would initially be empty. Other than that, your problem is caused by object slicing (I'm assuming that you are push_back()ing GuidedTours into the vector).

进行对象切片时,您仅存储 Tour GuidedTour 对象的$ c>部分,这就是为什么您看到 Tour :: display()的输出的原因code>。

When object slicing takes place, you are storing only the Tour part of your GuidedTour object(s), and that's the reason why you are seeing the output of Tour::display().

要解决您的问题,您需要通过使用(智能)指针并动态分配对象来多态存储对象。

To solve your problem, you need to store the objects polymorphically, by using (smart) pointers and dynamically-allocating your objects.

int main()
{
    vector <std::unique_ptr<Tour>> tourList;

    for(...) {
       tourList.push_back(std::make_unique<GuidedTour>(/* constructor parameters */));
       ...
       tourList.push_back(std::make_unique<Tour>(/* constructor parameters */));
    }

    for (unsigned int i = 0; i < tourList.size(); i++)
    {
        tourList[i]->display();
    }
}

请注意我正在使用 std :: unique_ptr / std :: make_unique 而不是原始的 new 指针。使用它们会极大地减轻您手动管理和删除对象的麻烦,有时会 [understatement] 是错误和未定义行为的原因。

Notice that I am using std::unique_ptr/std::make_unique and not raw newed pointers. Using them would greatly ease you of the problem of manually managing and deleteing your objects, which sometimes[understatement] are the cause of bugs and undefined behavior.

请注意,有些人可能建议您使用 boost :: ptr_vector 或类似的东西。听他们的话,尤其是当他们给您争论为什么他们比其他选择更好时。

Note that some people might suggest you to use boost::ptr_vector or something similar. Listen to them, especially if they give you arguments on why they are better than the alternatives.

这篇关于C ++覆盖的虚函数未调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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