Two pointers to the same obejct, two differnet values...

Discussion in 'C++' started by =?ISO-8859-1?Q?Mattias_Br=E4ndstr=F6m?=, Dec 18, 2003.

  1. Hello!

    Please consider the code below:

    class A {
    public:
    virtual void foo(int i) = 0;
    };

    class B {
    public:
    virtual void bar(int i) = 0;
    };

    class AB : public A, public B {
    public:
    void foo(int i) { }
    void bar(int i) { }
    };

    int main() {

    AB* ab = new AB();

    cout << ab << endl;
    cout << dynamic_cast<A*>(ab) << endl;
    cout << dynamic_cast<B*>(ab) << endl;

    return 0;
    }

    This gives me the output:

    0x8049d90
    0x8049d90
    0x8049d94

    Even though I point to the same object I get different values for my
    pointer. I can understand that this might happen when I use multiple
    inheritance and have more than 1 v-table (vtabl?).

    The question is this: can I rely on this behaivour and expect to get
    differnt pointers on all platforms or are there some platforms that
    might give me the same pointer no matter on how I look at an object?

    I suspect that this is not portable behaivour and that it's completely
    undefined and compiler depandant. It would be nice to hear your comments.

    Regards,
    Mattias
     
    =?ISO-8859-1?Q?Mattias_Br=E4ndstr=F6m?=, Dec 18, 2003
    #1
    1. Advertising

  2. On Thu, 18 Dec 2003 15:16:21 +0100, =?ISO-8859-1?Q?Mattias_Br=E4ndstr=F6m?= <> wrote:

    >class A {
    >public:
    > virtual void foo(int i) = 0;
    >};
    >
    >class B {
    >public:
    > virtual void bar(int i) = 0;
    >};
    >
    >class AB : public A, public B {
    >public:
    > void foo(int i) { }
    > void bar(int i) { }
    >};
    >
    >int main() {
    >
    > AB* ab = new AB();
    >
    > cout << ab << endl;
    > cout << dynamic_cast<A*>(ab) << endl;
    > cout << dynamic_cast<B*>(ab) << endl;
    >
    > return 0;
    >}
    >
    >This gives me the output:
    >
    >0x8049d90
    >0x8049d90
    >0x8049d94
    >
    >The question is this: can I rely on this behaivour and expect to get
    >differnt pointers on all platforms


    No. A C++ implementation might not even use v-tables.


    > or are there some platforms that
    >might give me the same pointer no matter on how I look at an object?


    There might be.
     
    Alf P. Steinbach, Dec 18, 2003
    #2
    1. Advertising

  3. Mattias Brändström wrote:
    ....
    >
    > The question is this: can I rely on this behaivour and expect to get
    > differnt pointers on all platforms or are there some platforms that
    > might give me the same pointer no matter on how I look at an object?


    I don't think the standard mandates what the "integer" value of the
    pointer should be. So I don't think you can rely on ANY behavior other
    than the basic pointer arithmetic and implicit pointer conversions.

    >
    > I suspect that this is not portable behaivour and that it's completely
    > undefined and compiler depandant. It would be nice to hear your comments.
    >


    exactly.
     
    Gianni Mariani, Dec 18, 2003
    #3
  4. =?ISO-8859-1?Q?Mattias_Br=E4ndstr=F6m?=

    Jumbo Guest

    "Mattias Brändström" <> wrote in message
    news:3fe1b8c9$0$163$...
    > Hello!
    >
    > Please consider the code below:
    >
    > class A {
    > public:
    > virtual void foo(int i) = 0;
    > };
    >
    > class B {
    > public:
    > virtual void bar(int i) = 0;
    > };
    >
    > class AB : public A, public B {
    > public:
    > void foo(int i) { }
    > void bar(int i) { }
    > };
    >
    > int main() {
    >
    > AB* ab = new AB();
    >
    > cout << ab << endl;
    > cout << dynamic_cast<A*>(ab) << endl;
    > cout << dynamic_cast<B*>(ab) << endl;
    >
    > return 0;
    > }
    >
    > This gives me the output:
    >
    > 0x8049d90
    > 0x8049d90
    > 0x8049d94
    >
    > Even though I point to the same object I get different values for my
    > pointer. I can understand that this might happen when I use multiple
    > inheritance and have more than 1 v-table (vtabl?).
    >
    > The question is this: can I rely on this behaivour and expect to get
    > differnt pointers on all platforms or are there some platforms that
    > might give me the same pointer no matter on how I look at an object?
    >
    > I suspect that this is not portable behaivour and that it's completely
    > undefined and compiler depandant. It would be nice to hear your comments.
    >
    > Regards,
    > Mattias
    >

    It also depends on the order you declare the inheritance i.e:

    class AB : public A, public B {
    ....
    };

    class AB : public B, public A {
    ....
    };

    These would produce different results.
    So it depends if the ISO specify this as an important and meaningfull aspect
    of the language.
    I think it should be documented in the standards as it is quite important to
    know whether you can rely on this or not.

    I think you have raised an interesting point here but unfortunately I do not
    know the answer.:eek:(
     
    Jumbo, Dec 18, 2003
    #4
    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. Darren Clark

    .net ddls'' in differnet directories?

    Darren Clark, Aug 19, 2003, in forum: ASP .Net
    Replies:
    3
    Views:
    2,245
    Richard Grimes [MVP]
    Sep 3, 2003
  2. Christian Vorholt

    MFC CDialogBar Obejct help!

    Christian Vorholt, Nov 9, 2003, in forum: C++
    Replies:
    1
    Views:
    632
    Dirk Feytons
    Nov 9, 2003
  3. alf
    Replies:
    3
    Views:
    318
    Steve Holden
    Nov 2, 2006
  4. =?ISO-8859-1?Q?Tor_Erik_S=F8nvisen?=

    Convert obejct string repr to actual object

    =?ISO-8859-1?Q?Tor_Erik_S=F8nvisen?=, Oct 8, 2007, in forum: Python
    Replies:
    2
    Views:
    265
    Steven D'Aprano
    Oct 8, 2007
  5. wzab
    Replies:
    5
    Views:
    175
    Oscar Benjamin
    May 16, 2013
Loading...

Share This Page