Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Access to private members by descendant classes

Posted on 1997-11-18
6
Medium Priority
?
180 Views
Last Modified: 2010-04-10
Suppose you have a certain class which has an attribute you want to maintain as private :

class A {

   private:

   int Example_Int;

   public:

   ...
};

Then you have a class which derives from the previous one, which has a certain member function.

class B : A {

   public:

   int Example_Function();
};

The member function just defined wants to have access to Example_Int without having to define it again on the derived class and without having to put it as public.

int B::Example_Function() {

   return Example_Int;           //Error
};


How to do it ?
0
Comment
Question by:sagitarian
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 2

Accepted Solution

by:
rigansen earned 100 total points
ID: 1173486
Have you tried this:

class B : public A {
public:
   int Example_Function();
};

not completly sure, but maybe... if not leave a comment.


0
 
LVL 2

Expert Comment

by:rigansen
ID: 1173487
seems that the fist try didn't work...

After some testing I think you have only two possibilities:

(1)

class A {
friend class B;
private:
   int Example_Int;
};



(2)

class A {
protected:
   int Example_Int;
};



the choice is up to you...

regards.

    rigansen.
0
 
LVL 22

Expert Comment

by:nietod
ID: 1173488
Protected is the way to go.  That is what protected is for.
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

Author Comment

by:sagitarian
ID: 1173489
Even using the public specifier for class A access on class B, it still depends wether class A's attribute you use is public or not. Nevertheless, one should use class A as public or else none of the class member could be seen.

The protected solution is the best one. This way it works, which is the main purpose, but also is more understandable.

Thanks for the answers.
0
 
LVL 2

Expert Comment

by:rigansen
ID: 1173490
Could someone explain ??

My answer was graded with a *D*  :(

Maybe I didn't hit the first time, but
wasn't it me who suggested in "Comment from rigansen..." the use of the 'protected' keyword ??

0
 

Author Comment

by:sagitarian
ID: 1173491
I graded the answer with a "B" because that was my evaluation of the "answer". Only then I saw the comment. I think that the comment should have been the answer in the first place.
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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. …
Article by: evilrix
Looking for a way to avoid searching through large data sets for data that doesn't exist? A Bloom Filter might be what you need. This data structure is a probabilistic filter that allows you to avoid unnecessary searches when you know the data defin…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
Suggested Courses

636 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