George2
George2

Reputation: 45821

virtual function issue

I am using native C++ with VSTS 2008. A quick question about virtual function. In my sample below, any differences if I declare 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;
    }
};

Upvotes: 4

Views: 116

Answers (2)

Ferruccio
Ferruccio

Reputation: 100748

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.

Upvotes: 4

Jay Zhu
Jay Zhu

Reputation: 1682

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

Upvotes: 9

Related Questions