ToHo
asked on
'Subclass' setting properties of 'Masterclass'
Hi
Is it possible for a sub-class to edit the properties of a class it's defined in ?
Example:
class CVar
{
int m_Value;
public:
class CValue
{
CValue& operator = (int value)
{
CVar::m_Value = value; //Not correct...
return *this
}
} Value;
};
main()
{
CVar Var;
Var.Value = 3; // Set CVar.m_Value
}
Any help appreciated.
Is it possible for a sub-class to edit the properties of a class it's defined in ?
Example:
class CVar
{
int m_Value;
public:
class CValue
{
CValue& operator = (int value)
{
CVar::m_Value = value; //Not correct...
return *this
}
} Value;
};
main()
{
CVar Var;
Var.Value = 3; // Set CVar.m_Value
}
Any help appreciated.
ASKER
I tried to declare CValue as a friend of CVar, but I still get the following error:
C2597: Illegal reference to non-static member 'CVar::m_Value'
C2597: Illegal reference to non-static member 'CVar::m_Value'
Sorry, I read too quickly. The problem here is that CValue::operator= is not public (in CValue). Just add a "public:" line before its declaration and the code should work.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Adding a m_pParent property to CValue worked just fine. Thanks!
class CVar
{
friend class CValue;
// ...
};