Connecting Tech Pros Worldwide Help | Site Map

how to cast stack-based class?

  #1  
Old July 23rd, 2005, 03:00 AM
Sowen
Guest
 
Posts: n/a
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, 03:00 AM
Larry Brasfield
Guest
 
Posts: n/a

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
Abstract class variables question tshad answers 20 November 20th, 2007 10:35 AM
Passing XmlDocument-derived class in a WebMethod olrt answers 1 July 14th, 2006 08:55 PM
System.InvalidCastException: Unable to cast object of type 'System.Xml.XmlDocument' to type 'System.String' John Smith answers 2 April 14th, 2006 08:55 PM
Unable to cast object of type System.__ComObject Fidias Gil de Montes answers 0 November 22nd, 2005 11:02 PM
Unable to cast object of type System.__ComObject Fidias Gil de Montes answers 0 September 16th, 2005 03:35 PM