Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how do I call a base class method from the derived class, if the derived class has a method with the same name?

Tags:

c++

this is what I want to be able to do: (pseudo code)

class DerivedClass : public BaseClass
{
    public Draw()
    {
        BaseClass.Draw()
    }
}

class BaseClass
{
    protected Draw();
}

Both draws have the same name and the same signature. The reason for wanting to do this is that sometimes I want my derived classes to have a draw function that simply calls the Base Classes draw, but at other times I want the derived class to choose when to call the base draw function or not. This meant that I can keep the class that I instantiate the derived classes in much cleaner, and can just call draw on all of them at all times. The derived classes themselves handle the nitty-gritty.

What exactly is the syntax for the BaseClass.Draw part? I thought you could actually just write that as it is, but the compiler is complaining, and it's not like I can just call Draw because the signatures are the same.

like image 388
Dollarslice Avatar asked Dec 22 '22 05:12

Dollarslice


2 Answers

The syntax is BaseClass::Draw().

like image 111
Kerrek SB Avatar answered Dec 23 '22 18:12

Kerrek SB


The syntax is

DeriveClass::Draw()
{
   BaseClass::Draw();
}
like image 31
parapura rajkumar Avatar answered Dec 23 '22 19:12

parapura rajkumar