Solved

'Subclass' setting properties of 'Masterclass'

Posted on 2002-06-27
5
223 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
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 100 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
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…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

920 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

16 Experts available now in Live!

Get 1:1 Help Now