Solved

'Subclass' setting properties of 'Masterclass'

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Unable to start eclipse ? 17 176
Embarcadero WebBroker REST server endpoint parameters 2 118
Why  my code (program) build with old compiler? 11 149
cmake and message 1 26
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
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.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

752 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