Solved

Inheritance question

Posted on 1998-07-15
4
183 Views
Last Modified: 2010-04-01
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...
0
Comment
Question by:moonlight
  • 2
4 Comments
 
LVL 8

Expert Comment

by:trestan
ID: 1167833
Declare friend function.
0
 
LVL 22

Expert Comment

by:nietod
ID: 1167834
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.
0
 
LVL 3

Author Comment

by:moonlight
ID: 1167835
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.
0
 
LVL 22

Accepted Solution

by:
nietod earned 10 total points
ID: 1167836
Okay.
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now