views:

59

answers:

2

Hello everyone,

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;
    }
};

thanks in advance, George

+10  A: 

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

Findekano
True, no difference but is much easier for people who haven't write the code to understand what is happening.
anthares
Cool, question answered!
George2
+1 for suggesting to always use virtual for clarity even though it isn't required.
Mark B
+4  A: 

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.

Ferruccio
Thanks for your reply.
George2
+1 for actually explaining why it makes not difference.
Space_C0wb0y