Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

'Subclass' setting properties of 'Masterclass'

Posted on 2002-06-27
5
Medium Priority
?
240 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 5

Expert Comment

by:yonat
ID: 7113606
Only if it is declared as its friend:

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

Author Comment

by:ToHo
ID: 7113639
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
ID: 7113708
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 400 total points
ID: 7113719
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
ID: 7115719
Adding a m_pParent property to CValue worked just fine. Thanks!
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
Suggested Courses

715 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