Link to home
Start Free TrialLog in
Avatar of ToHo
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.
Avatar of yonat
yonat

Only if it is declared as its friend:

class CVar
{
  friend class CValue;
  // ...
};
Avatar of ToHo

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'
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
Avatar of yonat
yonat

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of ToHo

ASKER

Adding a m_pParent property to CValue worked just fine. Thanks!