Solved

'Subclass' setting properties of 'Masterclass'

Posted on 2002-06-27
5
219 Views
Last Modified: 2012-05-04
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.
0
Comment
Question by:ToHo
  • 3
  • 2
5 Comments
 
LVL 5

Expert Comment

by:yonat
Comment Utility
Only if it is declared as its friend:

class CVar
{
  friend class CValue;
  // ...
};
0
 
LVL 2

Author Comment

by:ToHo
Comment Utility
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'
0
 
LVL 5

Expert Comment

by:yonat
Comment Utility
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.
0
 
LVL 5

Accepted Solution

by:
yonat earned 100 total points
Comment Utility
And also inside CValue::operator= you need to specify the parent object of m_Value (ie, which CVar's m_Value you want to set). You can save a reference to the parent CVar inside CValue to do that (like in Java inner classes).
0
 
LVL 2

Author Comment

by:ToHo
Comment Utility
Adding a m_pParent property to CValue worked just fine. Thanks!
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now