Thread: Upcasting and identifying objects by address

  1. #1
    Registered User
    Join Date
    Dec 2004
    Posts
    20

    Upcasting and identifying objects by address

    Up until now I always thought that you can identify an object by it's address in memory. This doesn't hold for upcasted pointers to different base classes when using multiple inheritance. These pointers can have different addresses than the address of the derived object and seeing how MI works, I can understand why this is.

    But now I've read that you can get differing addresses even in a single inhertance chain. So if you do:

    Derived* dp = new Derived;
    Base* bp = derived;

    you have no guarantee that these pointers hold the same address, even when it's not MI. Is that correct?

  2. #2
    Registered User
    Join Date
    Dec 2004
    Posts
    20
    Any ideas?

  3. #3
    semi-colon generator ChaosEngine's Avatar
    Join Date
    Sep 2005
    Location
    Chch, NZ
    Posts
    597
    Quote Originally Posted by New++
    Derived* dp = new Derived;
    Base* bp = derived;

    you have no guarantee that these pointers hold the same address, even when it's not MI. Is that correct?
    that code won't even compile.

    but something like
    Code:
    Derived* dp = new Derived;
    Base* bp = dp;
    should mean that bp and dp have the same value
    "I saw a sign that said 'Drink Canada Dry', so I started"
    -- Brendan Behan

    Free Compiler: Visual C++ 2005 Express
    If you program in C++, you need Boost. You should also know how to use the Standard Library (STL). Want to make games? After reading this, I don't like WxWidgets anymore. Want to add some scripting to your App?

  4. #4
    Registered User
    Join Date
    Dec 2004
    Posts
    20
    Cough, well it was late when I wrote that.
    I also think that the address should be the same in such a case. Do you happen to know if this is guaranteed by the standard?

  5. #5
    semi-colon generator ChaosEngine's Avatar
    Join Date
    Sep 2005
    Location
    Chch, NZ
    Posts
    597
    I don't have a copy of the standard with me so I can't check, but I'd be extremely surprised if this was not the case.
    "I saw a sign that said 'Drink Canada Dry', so I started"
    -- Brendan Behan

    Free Compiler: Visual C++ 2005 Express
    If you program in C++, you need Boost. You should also know how to use the Standard Library (STL). Want to make games? After reading this, I don't like WxWidgets anymore. Want to add some scripting to your App?

Popular pages Recent additions subscribe to a feed