Re: I want something weird

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance

From: Sergey Kochkarev (kochkarev_at_pisem.net)
Date: 10/18/04


Date: 18 Oct 2004 00:56:41 -0700


"Lisa Pearlson" <no@spam.plz> wrote in message news:<#Ee0#0LtEHA.3004@TK2MSFTNGP10.phx.gbl>...
> Hi,
>
> Imagine I have a class:
>
> class CSomeClass
> {
> public:
> int m_iSomeClass;
> int GetValue() { return m_iSomeClass; }
> }
>
>
> I now subclass it:
>
> class CSubClass : public CSomeClass
> {
> public:
> int m_iSubClass;
> }
>
> I do not wish to add a new member variable, but RENAME it.
> So, without redifining GetValue, I wish CSubClass::GetValue to return
> m_iSubClass instead of m_iSomeClass.
>
> The reason why I want a type of renaming is because for example I have an
> existing struct or class:
> CRect
> it has members "left, top, right, bottom".
>
> I wish to alias it through typedef or subclassing, like this:
> class CRange : public CRect {
> public:
> long xmin, ymin, xmax, ymax;
> }
>
> So I rename the members left to xmin etc. simply because this better
> relflects what it is used for. But I still get to use functions such as
> Width(), Height() and other CRect member functions without having to
> redefine all of them and basically rewrite the whole thing.
>
> This is just an example.. in reality I wish to do this with bigger more
> complicated classes.. I just wish to rename members.
> Is there any way to do this? perhaps something with virtual keyword?
>
> Lisa

There are no vertual member variables in C++, sorry. And the whole
idea of renaming variables is somewhat strange - you should not expose
public variables at all if you want to follow OOP. Still if you want
to and don't bother about extra 16 bytes, here you are a solution:

class CRenamedRect : public CRect
{
public:
 CRenamedRect() : CRect(),
   m_minx(left), m_miny(top), m_maxx(right), m_maxy(bottom)
 {
 }

 int& m_minx;
 int& m_miny;
 int& m_maxx;
 int& m_maxy;

};



Relevant Pages

  • Re: CLOS and C++
    ... > But Scott Meyers is not advocating making all class member variables ... (defclass-with-privates foo () ...
    (comp.lang.lisp)
  • Re: Visual C Cdialog.DoModal()
    ... The fast solution is to simply declare the dialog in the ... But unless this function needs access to class member variables (in which case ... Essentially, there is no need, as far as I can tell, to have the dialog member in the ... without a class instance there simply isn't ANY member variable you can talk about. ...
    (microsoft.public.vc.mfc)
  • Re: The reality of Topmind (Was: Topic-Organized Object-Oriented Programming)
    ... It is, simply, classifying all of a class' member variables and member ... suppose the class is "Human" (a class with member functions ... "hierarchy" and tree are the same goddam ...
    (comp.object)
  • Re: CButton casts
    ... been called because OnInitDialog() has not been called; ... can't set any of this earlier as the hWnd of the control member variables ... So it seems it is necessary to intialise some temporary member variables ...
    (microsoft.public.vc.mfc)
  • Re: class instances and threads
    ... >> I'd like to know if i need mutexs to lock the use of ... >> member methods of a class instance shared between ... Local variables of a method are not shared data, ...
    (comp.lang.cpp)