LunchMarble
LunchMarble

Reputation: 5141

Function overriding in native C++

Is the following code the correct way to call a base class's overridden function from a derived class's function?:

#include "stdafx.h"
#include <iostream>

class BaseClass
{
public:
    virtual void foo()
    {
        std::cout << "BaseClass::foo()" << std::endl;
    }
};
class DerivedClass : BaseClass
{
public:
    virtual void foo()
    {
        __super::foo();

        std::cout << "DerivedClass::foo()" << std::endl;
    }
};
int main()
{
    DerivedClass* dc = new DerivedClass();

    dc->foo();

    delete dc;

    return 0;
}

Upvotes: 1

Views: 155

Answers (2)

Cheers and hth. - Alf
Cheers and hth. - Alf

Reputation: 145279

No, your call using __super::foo() is employing a Microsoft language extension.

Instead you can write BaseClass::foo().

Or if you have a typedef BaseClass Base, then Base::foo().

Cheers & hth.,

Upvotes: 6

zienkikk
zienkikk

Reputation: 2414

BaseClass::foo()is what you're looking for

Upvotes: 1

Related Questions