3

I have a parent class and I have 2 publicly derived classes from that parent class. eg.

                     class Parent
                       |      |
                       |      |
                       |      |
              class derived1   class derived2.

Question: I would like to access the private members of one derived class from another derived class. How do I do this?

The way I have it now is as follows: Passing the cDerived1 object as a parameter to the ctor of cDerived2. If I do it this way, then I have to declare cDerived2 as a friend of cDerived1 and also include cDerived1.h inside cDerived2.h

#include cParent.h
#include cDerived1.h
#include cDerived2.h

void main (){

// Instantiate a cDerived1 object
Derived1 dev1();

// Instantiate a cDerived2 object. The cDerived2 object will need access to the   
// private members of cDerived1. So pass dev1 by reference to dev2 ctor.
Derived2 dev2(dev1);  
}

Is this the right way to do it or am I doing something very blatantly wrong ??

Thanks.

In response to Paul's comment: I already have the shared code in the parent class as shown below.

cParent.h
class cparent{

public:
// ctor
// dtor
protected:
int* pArr;
};

 cDerived1.h
// derived1's header
#include "cParent.h"

class cDerived1 : public cParent{
public:
//
};


 cDerived2.h
// derived2's header
#include "cParent.h"
class cDerived2 : public cParent{
public:
// I want access to derived1's pArr member over here....How do I do this ?
rjm
  • 31
  • 4

2 Answers2

0

This is what the keyword friend is used for. It can be used for either functions or classes...in this case you would apply it to the class. The only way you can access protected members is through inheritance...i.e. a sub-class, or a friend class. I'm pretty sure the only way to access private members outside of the class is to use a friend function or class. Here is a relevant link from cplusplus.com

Here is their example (their second) of declaring a friend class.

class CSquare {
  private:
    int side;
  public:
    void set_side (int a)
      {side=a;}
    friend class CRectangle;  // here is the friend declaration 
};

So in your example you would use:

friend class Derived1; // place in Derived2 

friend class Derived2; // place in Derived1

Derived 1 would have access to Derived 2 private members, and Derived 2 would have access to Derived 1 private members.

I'm assuming you don't want to simply use a getter function.

Once it is declared a friend you can use:

Obj.theVar

instead of using a getter function like this:

Obj.getVar()

Community
  • 1
  • 1
0

Since pArr is in the base class, you can achieve this without needing the definition of cDerived1. Pass the object of cDerived1 as pointer of the cparent class and everything would be alright. Here is some sample code...

// cDerived2.h
// derived2's header
#include "cParent.h"
class cDerived2 : public cParent{
public:
// I want access to derived1's pArr member over here....How do I do this ?
void AccessPArr(cparent* pParent)
{
// access over here
}

int main()
{
cDerived1 der1;
cDerived2 der2;

der2.AccessPArr(&der1);
}
Ajit Vaze
  • 2,686
  • 2
  • 20
  • 24