Pointer to class equals pointer to base class

Discussion in 'C++' started by Alberto Luaces, Jun 19, 2006.

  1. Hi,

    can I always rely on the behaviour shown in the code? (A pointer to a class
    is always the same as the pointer obtained from its base class with RTTI)

    #include <iostream>

    class A{};
    class B : public A {};

    int main(){
    B b;

    std::cout << (&b == dynamic_cast<A*>(&b)) << "\n"; // Always true?
    return 0;
    }

    Thank you,

    Alberto
     
    Alberto Luaces, Jun 19, 2006
    #1
    1. Advertising

  2. Alberto Luaces

    Rolf Magnus Guest

    Alberto Luaces wrote:

    > Hi,
    >
    > can I always rely on the behaviour shown in the code? (A pointer to a
    > class is always the same as the pointer obtained from its base class with
    > RTTI)


    They don't need to point to the same address, but they must compare equal.
    Btw: That doesn't have anything to do with RTTI. The dynamic_cast will only
    add a runtime type check. If you replace it with a static_cast in your
    program, the result will be the same.

    >
    > #include <iostream>
    >
    > class A{};
    > class B : public A {};
    >
    > int main(){
    > B b;
    >
    > std::cout << (&b == dynamic_cast<A*>(&b)) << "\n"; // Always true?
    > return 0;
    > }
    >
    > Thank you,
    >
    > Alberto
     
    Rolf Magnus, Jun 19, 2006
    #2
    1. Advertising

  3. Alberto Luaces wrote:
    > Hi,
    >
    > can I always rely on the behaviour shown in the code? (A pointer to a class
    > is always the same as the pointer obtained from its base class with RTTI)
    >
    > #include <iostream>
    >
    > class A{};
    > class B : public A {};
    >
    > int main(){
    > B b;
    >
    > std::cout << (&b == dynamic_cast<A*>(&b)) << "\n"; // Always true?
    > return 0;
    > }


    This is always true (if the base class is not unique it will fail to
    compile though). What is not guaranteed to be true is

    (void *)&b == (void *)dynamic_cast<A*>(&b)
     
    Markus Schoder, Jun 19, 2006
    #3
  4. Rolf Magnus posted:

    > Alberto Luaces wrote:
    >
    >> Hi,
    >>
    >> can I always rely on the behaviour shown in the code? (A pointer to a
    >> class is always the same as the pointer obtained from its base class
    >> with RTTI)

    >
    > They don't need to point to the same address, but they must compare
    > equal.



    That's pretty nifty! So basically the following will always be true:


    &derived_object == static_cast<Base*>( &derived_object )


    But that following need not necessarily be true:


    static_cast<void*>(&derived_object) ==

    static_cast<void*>( static_cast<Base*>( &derived_object ) )



    --

    Frederick Gotham
     
    Frederick Gotham, Jun 19, 2006
    #4
  5. Rolf Magnus posted:

    > Alberto Luaces wrote:
    >
    >> Hi,
    >>
    >> can I always rely on the behaviour shown in the code? (A pointer to a
    >> class is always the same as the pointer obtained from its base class
    >> with RTTI)

    >
    > They don't need to point to the same address, but they must compare
    > equal.



    That's pretty nifty! So basically the following will always be true:


    &derived_object == static_cast<Base*>( &derived_object )


    But that following need not necessarily be true:


    static_cast<void*>(&derived_object) ==

    static_cast<void*>( static_cast<Base*>( &derived_object ) )



    --

    Frederick Gotham
     
    Frederick Gotham, Jun 19, 2006
    #5
  6. Rolf Magnus posted:

    > Alberto Luaces wrote:
    >
    >> Hi,
    >>
    >> can I always rely on the behaviour shown in the code? (A pointer to a
    >> class is always the same as the pointer obtained from its base class
    >> with RTTI)

    >
    > They don't need to point to the same address, but they must compare
    > equal.



    That's pretty nifty! So basically the following will always be true:


    &derived_object == static_cast<Base*>( &derived_object )


    But that following need not necessarily be true:


    static_cast<void*>(&derived_object) ==

    static_cast<void*>( static_cast<Base*>( &derived_object ) )



    --

    Frederick Gotham
     
    Frederick Gotham, Jun 19, 2006
    #6
  7. Alberto Luaces

    Fraser Ross Guest

    A pointer to a base class is not necessarily equal to a pointer to a
    class derived from it. You could see the free item 27 of Effective C++
    3rd edition for an explanation.

    Fraser.


    "Alberto Luaces"
    > Hi,
    >
    > can I always rely on the behaviour shown in the code? (A pointer to a

    class
    > is always the same as the pointer obtained from its base class with

    RTTI)
    >
    > #include <iostream>
    >
    > class A{};
    > class B : public A {};
    >
    > int main(){
    > B b;
    >
    > std::cout << (&b == dynamic_cast<A*>(&b)) << "\n"; // Always

    true?
    > return 0;
    > }
    >
    > Thank you,
    >
    > Alberto




    Posted Via Usenet.com Premium Usenet Newsgroup Services
    ----------------------------------------------------------
    ** SPEED ** RETENTION ** COMPLETION ** ANONYMITY **
    ----------------------------------------------------------
    http://www.usenet.com
     
    Fraser Ross, Jun 19, 2006
    #7
    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. Edward A Thompson
    Replies:
    4
    Views:
    538
    Tony Morris
    Feb 11, 2004
  2. tirath
    Replies:
    3
    Views:
    716
    Ivan Vecerina
    Oct 12, 2003
  3. Alf P. Steinbach
    Replies:
    6
    Views:
    557
    John Carson
    Sep 3, 2005
  4. Hicham Mouline
    Replies:
    1
    Views:
    603
    Victor Bazarov
    Apr 20, 2009
  5. , India
    Replies:
    8
    Views:
    980
    gwowen
    Aug 18, 2010
Loading...

Share This Page