C ++: access to the method of grandparents

Why doesn't this work, and what would be a good alternative?

class Grandparent
{
    void DoSomething( int number );
};

class Parent : Grandparent
{
};

class Child : Parent
{
    void DoSomething()
    {
        Grandparent::DoSomething( 10 ); // Does not work.
        Parent::DoSomething( 10 ); // Does not work.
    }
};
+3
source share
2 answers
class Grandparent
{
protected:
    void DoSomething( int number );
};

class Parent : protected Grandparent
{
};

class Child : Parent
{
    void DoSomething()
    {
        Grandparent::DoSomething( 10 ); //Now it works
        Parent::DoSomething( 10 ); // Now it works.
    }
};

At a minimum, it should look like this. Things are private by default when working with classes, this includes subclasses.

http://codepad.org/xRhc5ig4

There is a complete example of compiling and running.

+11
source

I assume that the code did not compile - and the main source of the error was missing a return type specifier for the method Child::DoSomething().

0
source

Source: https://habr.com/ru/post/1785669/


All Articles