Reputation: 13
Although I made the members of both base and derived classes public
, Why does the conversion says that it is inaccessible?
class B
{
public:
int var;
};
class D: private B
{
public:
int var;
};
int main()
{
D d;
Base &b = d; //my error
}
Upvotes: 1
Views: 128
Reputation: 96241
You can't convert from a derived class to a non-public base class (well, outside the class itself anyway). Private inheritance is typically used as a form of composition, not for substitution so there's no need to convert to the base. In your case it looks like you want substitution so you should use public inheritance.
Upvotes: 1
Reputation: 6823
Try this:
#include <iostream>
class B
{
public:
int var;
};
class D: public B
{
public:
int var;
};
int main()
{
D d;
B &b = d; //my error
}
What you are trying to do won't work with private inheritance. Also note that your base class is known as B
and not Base
.
Regards,
Dennis M.
Upvotes: 0