Connecting Tech Pros Worldwide Help | Site Map

how to cast stack-based class?

 
LinkBack Thread Tools Search this Thread
  #1  
Old July 23rd, 2005, 02:00 AM
Sowen
Guest
 
Posts: n/a
Default how to cast stack-based class?

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



  #2  
Old July 23rd, 2005, 02:00 AM
Larry Brasfield
Guest
 
Posts: n/a
Default 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.


 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,989 network members.