Link to home
Start Free TrialLog in
Avatar of moonlight
moonlight

asked on

Inheritance question

Is there any way for a subclass to access private members in its baseclass directly? Like if they were declared public. But I dont want other classes to have access to them. If I make public functions returning the private members, I havent
achieved anything really, right? It's visible to all other classes, and the subclasses get access to them in the same way as others...
Avatar of trestan
trestan
Flag of Canada image

Declare friend function.
Avatar of nietod
nietod

You can declare derived class or some of its functions to be friends as trestan suggested.  

However, depending on your needs you can declare some of the base class's members as "protected" rather than "private".  That is what protected is for.  However if a member is protected, all publically derived classes will have access to it.  Using friend you could give access to some derived classes and not others.  With protected you are giving access to all.
Avatar of moonlight

ASKER

I'm sorry trestan, but if you had taken the time to explain like nietod, you'd have had 60 more points now, would have been easy points too.
nietod, answer please, and cash in your points.
ASKER CERTIFIED SOLUTION
Avatar of nietod
nietod

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial