Connecting Tech Pros Worldwide Help | Site Map

how to cast stack-based class?

Sowen
Guest
 
Posts: n/a
#1: Jul 23 '05
Hi, all

for example

in my parent class, I define "virtual bool operator==(const parent &other);"

In my children class, I need to override this method, how can I convert
"parent &other" to children so that I can access

((children)other).getChildName()

I know it's not correct in C++, can I do that ?

Thanks !



--
Best Regards!
Sowen Cheung
http://com.angGoGo.com
http://www.angGoGo.com
http://biz.angGoGo.com


Larry Brasfield
Guest
 
Posts: n/a
#2: Jul 23 '05

re: how to cast stack-based class?


"Sowen" <sowen@anggogo.com> wrote in message
news:d12abd$5mv$1@canopus.cc.umanitoba.ca...[color=blue]
> Hi, all[/color]
Hi.

To begin, the answer to your question does
not depend on anything related to the usual
"stack" in C++ discussions.
[color=blue]
> for example
>
> in my parent class, I define "virtual bool operator==(const parent &other);"
>
> In my children class, I need to override this method, how can I convert "parent &other" to children so that I can access
>
> ((children)other).getChildName()[/color]

I will assume that 'children' is derived from 'parent',
and that there may be siblings to 'children', (such
as 'bastard_children', 'foster_children', etc.).

Unless you have some way of knowing for sure that
instances of different classes will not be compared
using your operator==(), use code like this:
children * validated_other = dynamic_cast<children *>(& other);
if (validate_other == 0)
return false;
// Whatever is needed to determine and return equality.
If you do know for sure that only instances of the
same type will be compared, you can change the
dynamic_cast to a static_cast and discard the test.
[color=blue]
> I know it's not correct in C++, can I do that ?[/color]

Yes, but not as you first wrote.

--
--Larry Brasfield
email: donotspam_larry_brasfield@hotmail.com
Above views may belong only to me.


Closed Thread