C++ inheritance - inaccessible base?

C++Inheritance

C++ Problem Overview


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

C++ Solutions


Solution 1 - C++

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 - C++

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

class Bar : public Foo

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionbandaiView Question on Stackoverflow
Solution 1 - C++Andrew NoyesView Answer on Stackoverflow
Solution 2 - C++Jim BuckView Answer on Stackoverflow