开发者

virtual function issue

开发者 https://www.devze.com 2022-12-22 10:18 出处:网络
I am using native C++ with VSTS 2008. A quick question about virtual function. In my sample below, any differences if I d开发者_运维问答eclare Foo as \"virtual void Foo()\" or \"void Foo()\" in class

I am using native C++ with VSTS 2008. A quick question about virtual function. In my sample below, any differences if I d开发者_运维问答eclare Foo as "virtual void Foo()" or "void Foo()" in class Derived? Any impact to any future classes which will derive from class Derived?

class Base
{
public:

    Base()
    {
    }

    virtual void Foo()
    {
        cout << "In base" << endl;
    }
};

class Derived : public Base
{
public:

    Derived()
    {

    }

    void Foo()
    {
        cout << "In derived " << endl;
    }
};


No difference. But for the sake of readbility I always keep the virtual whenever it is.


No, as long as it has the same signature as the member function in the base class, it will automatically be made virtual. You should make it explicitly virtual, however, to avoid confusing anyone reading the code.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号