Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > how to cast stack-based class?

Reply
Thread Tools

how to cast stack-based class?

 
 
Sowen
Guest
Posts: n/a
 
      03-13-2005
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


 
Reply With Quote
 
 
 
 
Larry Brasfield
Guest
Posts: n/a
 
      03-13-2005
"Sowen" <(E-Mail Removed)> wrote in message
news:d12abd$5mv$(E-Mail Removed)...
> Hi, all

Hi.

To begin, the answer to your question does
not depend on anything related to the usual
"stack" in C++ discussions.

> 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 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.

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


Yes, but not as you first wrote.

--
--Larry Brasfield
email: http://www.velocityreviews.com/forums/(E-Mail Removed)
Above views may belong only to me.


 
Reply With Quote
 
 
 
Reply

Thread Tools

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
Is the result of valid dynamic cast always equal to the result ofcorrespondent static cast? Pavel C++ 7 09-18-2010 11:35 PM
error C2440: 'return' : cannot convert from 'const char *' to 'const unsigned short *' Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast Abhijit Bhadra C++ 2 12-01-2004 04:43 PM
malloc - to cast or not to cast, that is the question... EvilRix C Programming 8 02-14-2004 12:08 PM
to cast or not to cast malloc ? MSG C Programming 38 02-10-2004 03:13 PM



Advertisments