首页 > c++ > 调用C ++基本方法而不是派生方法

调用C ++基本方法而不是派生方法 (C++ base method called instead of derived method)

2019-03-02 c++inheritance

问题

所以,我知道这样的问题很多,但其他答案似乎表明这应该有用,所以我想我会特别询问我的代码。我在.NET和Java上花了更多的时间,所以也许我忘记了什么。

我正在创建一个基本小部件和一组特定的小部件(按钮,其他图形项)。我正在尝试遍历一个链接的小部件列表,我想要的是调用该特定小部件的特定呈现方法。下面是演示我的问题的简化代码,在我第一次调用Render时,调用了特定的渲染函数。如果我将cGeneralWidget指针传递给调用Render对象的函数,则调用基本方法。我想,只要我发送一个指针,C ++就足够聪明,可以知道对象到底是什么,并调用重写方法。

否则,是否有正确的方法来处理基础对象的集合并适当地调用派生方法?

谢谢。

class cGeneralWidget
{
public:
    void Render()
    {
        int a = 99;
    }
};

class cSpecificWidget : public cGeneralWidget
{
public:
    void Render()
    {
        int a = 99;
    }
};

void GeneralRenderingFunction(cGeneralWidget * b)
{
    b->Render();   // <-- calls the base function
}

int main(int argc, char* argv[])
{

    cSpecificWidget wd;
    wd.Render();   // <-- calls the derived function
    GeneralRenderingFunction(&wd);  

    return 0;
}

解决方法

在你的基类

class cGeneralWidget
{
public:
    virtual void Render()
    {
        int a = 99;
    }
};

渲染必须是虚拟的,因此它会覆盖它

注意:虚函数是一个成员函数,它在基类中声明并由派生类重新定义(Overriden)。当您使用指针或对基类的引用引用派生类对象时,可以为该对象调用虚函数并执行派生类的函数版本。

问题

So, I know something like this is asked a lot, but the other answers seem to indicate that this should work, so I thought I'd ask about my code specifically. I spend more time in .NET and Java, so maybe I'm forgetting something.

I'm creating a base widget and a set of specific widgets (buttons, other graphical items). I'm trying to loop through a linked list of widgets and what I would like is for the specific rendering method for that specific widget to be called. Below is simplified code that demonstrates my problem, on my first call to Render, the specific render function is called. If I pass a cGeneralWidget pointer to a function which calls the Render object, the base method was called. I thought, as long as I send a pointer, C++ is smart enough to know what the object really is and call the overridden method.

Otherwise, whats the correct way to deal with a collection of base objects and call the derived methods as appropriate?

Thank you.

class cGeneralWidget
{
public:
    void Render()
    {
        int a = 99;
    }
};

class cSpecificWidget : public cGeneralWidget
{
public:
    void Render()
    {
        int a = 99;
    }
};

void GeneralRenderingFunction(cGeneralWidget * b)
{
    b->Render();   // <-- calls the base function
}

int main(int argc, char* argv[])
{

    cSpecificWidget wd;
    wd.Render();   // <-- calls the derived function
    GeneralRenderingFunction(&wd);  

    return 0;
}

解决方法

in your base class

class cGeneralWidget
{
public:
    virtual void Render()
    {
        int a = 99;
    }
};

Render has to be virtual so it overrides it

Note : A virtual function a member function which is declared within base class and is re-defined (Overriden) by derived class. When you refer to a derived class object using a pointer or a reference to the base class, you can call a virtual function for that object and execute the derived class’s version of the function.

相似信息