Reputation: 9841
I have simple code, which I assume to be failed.
I have privately inherit Shield
from Sealer
, and even Shield
is not it's friend
, still I am able to create object of Shield
.
class Sealer
{
public:
Sealer()
{
cout<<"base constructor;"<<endl;
}
};
class Shield : private Sealer
{
public:
void p()
{
cout<<"P gets called;"<<endl;
}
};
int main()
{
Shield d; //success here
d.p(); // here too
return 0;
}
How it is possible? Base class constructor should not be accessible. Isn't it?
I am using Visual Studio 2012.
Upvotes: 0
Views: 122
Reputation: 206567
When you use private
inheritance, you cannot access the base class functionality through the derived class. You cannot create a base class pointer or a reference from the derived class.
class Sealer
{
public:
Sealer() {}
void p()
{
cout<<"P gets called;"<<endl;
}
};
class Shield : private Sealer
{
};
int main()
{
Shield d;
d.p(); // Not allowed.
Sealer& p = d; // Not allowed.
return 0;
}
Upvotes: 0
Reputation: 1861
class Shield : private Sealer
means that everything in Sealer
is kept private within Shield
; it cannot be seen outside Shield
or in classes derived from it.
It does not magically go back and make Sealer
's constructor private so that Shield
cannot access it. What would be the point of private inheritance if the child class could not access anything from the base class? It would do exactly nothing.
Upvotes: 1
Reputation: 36391
This does not means that Sealer
is private relative to Shield
(Sealer
member access from Shield
is controlled via access category declaration), it simply means that the inheritance is private, means that this is not externally observable (you can manipulate Shield
as you like but not has Shield
instance).
Upvotes: 0