C++ related issue
-
Friends, Suppose we pass an object of a certain class as a reference to two other classes. It means that the two classes have a "same" copy of an object. This is a type of "composition" Now insted of "composition", i want to get the same effect using "inheritance" OR i want my derived classes to inherit from "same" base class. Same base class means that the same base subobject. If one class make any change in the data of its parent, then the change should get reflected in the second class as its parent is same as that of first class. How is it possible ???
-
Friends, Suppose we pass an object of a certain class as a reference to two other classes. It means that the two classes have a "same" copy of an object. This is a type of "composition" Now insted of "composition", i want to get the same effect using "inheritance" OR i want my derived classes to inherit from "same" base class. Same base class means that the same base subobject. If one class make any change in the data of its parent, then the change should get reflected in the second class as its parent is same as that of first class. How is it possible ???
-
What do you actually want to do? Are you a student quoting from your exercise or something? You should rephrase your question so it actually makes sense.
Consider the following code:
class Parent
{
public:
SetValue(int a)
{
m_value = a;
}private:
int m\_value
};
class Brother:public Parent
{
brother()
{
SetValue(12)
{
};class Sister:public Parent
{
Sister()
{
SetValue(8)
}
};As can be seen above, the two classes "Brother" and "Sister" are derived from the same base class "Parent" Both the children classes have their own copy of "Parent". If the class "Brother" makes any change in the data member of "Parent", then the change will not effect the "Parent" of "Sister" as "Sister" has its own "Parent" What i want is that, both the children classes should share the "same" parent. And if "Brother" changes the data present in its "Parent", then the data of the "Parent" of "Sister" should also gets changed automatically as both "Brother" and "Sister" will have same subobject "Parent". How can i achieve this goal.
-
Consider the following code:
class Parent
{
public:
SetValue(int a)
{
m_value = a;
}private:
int m\_value
};
class Brother:public Parent
{
brother()
{
SetValue(12)
{
};class Sister:public Parent
{
Sister()
{
SetValue(8)
}
};As can be seen above, the two classes "Brother" and "Sister" are derived from the same base class "Parent" Both the children classes have their own copy of "Parent". If the class "Brother" makes any change in the data member of "Parent", then the change will not effect the "Parent" of "Sister" as "Sister" has its own "Parent" What i want is that, both the children classes should share the "same" parent. And if "Brother" changes the data present in its "Parent", then the data of the "Parent" of "Sister" should also gets changed automatically as both "Brother" and "Sister" will have same subobject "Parent". How can i achieve this goal.
Jamal Jamshed wrote: How can i achieve this goal.
**static** int m_value;
-
Friends, Suppose we pass an object of a certain class as a reference to two other classes. It means that the two classes have a "same" copy of an object. This is a type of "composition" Now insted of "composition", i want to get the same effect using "inheritance" OR i want my derived classes to inherit from "same" base class. Same base class means that the same base subobject. If one class make any change in the data of its parent, then the change should get reflected in the second class as its parent is same as that of first class. How is it possible ???
- Create a base class that all related classes are derived from. 2) Make any member variables that you wish to be common to all derived classes static members. 3) If you are going to destroy (free) any of the members in the destuctor then you need to add a static reference counter member variable that is incremented in the constructor and decremented in the destructor.
// In header
class MyClass
{
static int m_nRefCount;
static int m_nMem1;
static whatever* m_pWhatEver;
...................
public: // constructors / destructors / accessors / manipulators / ect...
...................
};// In module / code file
int MyClass::m_nRefCount = 0;
whatever MyClass::m_pWhatEver = NULL;MyClass::MyClass()
{
++m_nRefCount;
}MyClass::~MyClass()
{
if( --m_nRefCount < 1 )
{
if( m_pWhatEver is valid )
delete m_pWhatEver;
}
}:-DHave a wonderful day. INTP
-
Consider the following code:
class Parent
{
public:
SetValue(int a)
{
m_value = a;
}private:
int m\_value
};
class Brother:public Parent
{
brother()
{
SetValue(12)
{
};class Sister:public Parent
{
Sister()
{
SetValue(8)
}
};As can be seen above, the two classes "Brother" and "Sister" are derived from the same base class "Parent" Both the children classes have their own copy of "Parent". If the class "Brother" makes any change in the data member of "Parent", then the change will not effect the "Parent" of "Sister" as "Sister" has its own "Parent" What i want is that, both the children classes should share the "same" parent. And if "Brother" changes the data present in its "Parent", then the data of the "Parent" of "Sister" should also gets changed automatically as both "Brother" and "Sister" will have same subobject "Parent". How can i achieve this goal.
The magic words you are looking for are "virtual inheritance[^]."