Solved

'Subclass' setting properties of 'Masterclass'

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Handle Exceptions during instantiation 28 415
C++ assignment question 7 199
Getting IP address 8 79
gdb doesn't stop on breakpoint 2 68
Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
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…
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 technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

777 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