473,385 Members | 1,546 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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

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
3 1245
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
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

"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Ilan | last post by:
Hi all I need to add data from two Excel sheets (both on the same workbook) to an existing table in my SQL DB. The problem is that each sheet holds different fields for the same record, though...
96
by: subramanian | last post by:
I have been thinking that all pointers(to any obejct) have the same size. The size of a pointer is the size of an int. This is beause a memory location is addressed by an int. Is that right/wrong?
69
by: Yee.Chuang | last post by:
When I began to learn C, My teacher told me that pointer is the most difficult part of C, it makes me afraid of it. After finishing C program class, I found that all the code I wrote in C contains...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.