how to cast stack-based class?

Discussion in 'C++' started by Sowen, Mar 13, 2005.

  1. Sowen

    Sowen Guest

    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
     
    Sowen, Mar 13, 2005
    #1
    1. Advertising

  2. "Sowen" <> wrote in message
    news:d12abd$5mv$...
    > 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:
    Above views may belong only to me.
     
    Larry Brasfield, Mar 13, 2005
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. MSG

    to cast or not to cast malloc ?

    MSG, Feb 6, 2004, in forum: C Programming
    Replies:
    38
    Views:
    1,096
    Dan Pop
    Feb 10, 2004
  2. EvilRix
    Replies:
    8
    Views:
    664
    Martin Dickopp
    Feb 14, 2004
  3. Surinder Singh
    Replies:
    1
    Views:
    1,230
    Richard Bos
    Dec 20, 2007
  4. Pavel
    Replies:
    7
    Views:
    550
    Pavel
    Sep 19, 2010
  5. rolo
    Replies:
    5
    Views:
    561
    Jean-Hugues ROBERT
    Apr 5, 2004
Loading...

Share This Page