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

  • Thread starter =?ISO-8859-1?Q?Mattias_Br=E4ndstr=F6m?=
  • Start date
?

=?ISO-8859-1?Q?Mattias_Br=E4ndstr=F6m?=

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
 
A

Alf P. Steinbach

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.
 
G

Gianni Mariani

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.
 
J

Jumbo

Mattias Brändström said:
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:(
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top