C++ inheritance - inaccessible base?

111,135

Solution 1

You have to do this:

class Bar : public Foo
{
    // ...
}

The default inheritance type of a class in C++ is private, so any public and protected members from the base class are limited to private. struct inheritance on the other hand is public by default.

Solution 2

By default, inheritance is private. You have to explicitly use public:

class Bar : public Foo

Share:
111,135

Related videos on Youtube

bandai
Author by

bandai

Updated on July 18, 2020

Comments

  • bandai
    bandai almost 4 years

    I seem to be unable to use a base class as a function parameter, have I messed up my inheritance?

    I have the following in my main:

    int some_ftn(Foo *f) { /* some code */ };
    Bar b;
    some_ftn(&b);
    

    And the class Bar inheriting from Foo in such a way:

    class Bar : Foo
    {
    public:
        Bar();
        //snip
    
    private:
        //snip
    };
    

    Should this not work? I don't seem to be able to make that call in my main function

  • Travis Gockel
    Travis Gockel over 13 years
    To expand: In a class , inheritance is private . In a struct , inheritance is public by default.
  • Mital Vora
    Mital Vora over 3 years
    I have seen some recent code with class Bar : virtual Foo, I could not find any documentation and details about virtual mode of inheritance, does anybody know about it explain it ?