Solved

Access to private members by descendant classes

Posted on 1997-11-18
6
161 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
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
ScreenConnect 6.0 Free Trial

Check out the updates in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI that improves session organization and overall user experience. See the enhancements for yourself!

 

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

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

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

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
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.

777 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