By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,305 Members | 1,588 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,305 IT Pros & Developers. It's quick & easy.

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

P: n/a
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

Jul 22 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
On Thu, 18 Dec 2003 15:16:21 +0100, =?ISO-8859-1?Q?Mattias_Br=E4ndstr=F6m?= <br****@ludd.luth.se> 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.

Jul 22 '05 #2

P: n/a
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.

Jul 22 '05 #3

P: n/a

"Mattias Brändström" <br****@ludd.luth.se> wrote in message
news:3f*********************@news.luth.se...
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.:o(


Jul 22 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.