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 ?