Re: I want something weird
From: Sergey Kochkarev (kochkarev_at_pisem.net)
Date: 10/18/04
- Next message: rsobies: "nntp"
- Previous message: Bredal Jensen: "Need to add dynamic submenu."
- In reply to: Lisa Pearlson: "I want something weird"
- Next in thread: Dave: "Re: I want something weird"
- Messages sorted by: [ date ] [ thread ]
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;
};
- Next message: rsobies: "nntp"
- Previous message: Bredal Jensen: "Need to add dynamic submenu."
- In reply to: Lisa Pearlson: "I want something weird"
- Next in thread: Dave: "Re: I want something weird"
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|