Solved

Access to private members by descendant classes

Posted on 1997-11-18
6
157 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
  • 3
  • 2
6 Comments
 
LVL 2

Accepted Solution

by:
rigansen earned 100 total points
Comment Utility
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
Comment Utility
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
Comment Utility
Protected is the way to go.  That is what protected is for.
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

Author Comment

by:sagitarian
Comment Utility
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
Comment Utility
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
Comment Utility
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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
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…

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

9 Experts available now in Live!

Get 1:1 Help Now