473,322 Members | 1,314 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,322 software developers and data experts.

class member acces through pointer vs object

While reading Inside the C++ object model I came through the following
paragraph

Point3d origin, *ptr = &origin;
A) origin.x = 0.0;
B) ptr->x = 0.0;

The book says "A & B statements performs equivalently if x is a member
of a struct, class, single inheritance hierarchy, or multiple
inheritance hierarchy" This is because compiler knows the offset of
the member at compile time.

My doubt is, How can the compiler know the offset of x in case of B
for multiple inheritance.
suppose we have
class Base_1{public: int i;}
class Base_2{public: int x;}
class Derived: public Base_1, public Base_2: {public: int k;}

now the offset of x will be different in Base_2 and Derived, and the
ptr may refer to any kind of object at run time, so how can we know
the offset at compile time.

I mean the access through pointer must be slower in the above case of
Multiple inheritance. Please correct me if I am wrong.
Jul 18 '08 #1
3 1431
Rahul <ra*********@lucent.comwrites:
While reading Inside the C++ object model I came through the following
paragraph

Point3d origin, *ptr = &origin;
A) origin.x = 0.0;
B) ptr->x = 0.0;

The book says "A & B statements performs equivalently if x is a member
of a struct, class, single inheritance hierarchy, or multiple
inheritance hierarchy" This is because compiler knows the offset of
the member at compile time.

My doubt is, How can the compiler know the offset of x in case of B
for multiple inheritance.
suppose we have
class Base_1{public: int i;}
class Base_2{public: int x;}
class Derived: public Base_1, public Base_2: {public: int k;}

now the offset of x will be different in Base_2 and Derived, and the
ptr may refer to any kind of object at run time, so how can we know
the offset at compile time.

I mean the access through pointer must be slower in the above case of
Multiple inheritance. Please correct me if I am wrong.
This is because when you assign a pointer to a derived multiply
inherinting object to a variable pointing to one of the superclass, an
offset is added.
Derived* d=new Derived();
Base_1* b1=d;
Base_2* b2=d;

+--------------------+
d -----| Base_1 members |<----- b1
+--------------------+
| Base_2 members |<----- b2
+--------------------+
| Derived members |
+--------------------+

So when you access d->x, it knows that d points to a Derived and that the offset to x is
sizeof(Base_1)+(&(((Base_2*)0)->x))-((Base_2*)0).

When you access b1->x of course, the compiler knows that there is no x
in b1 so it gives an error (even when b1 actually points to a d that
has an x; that's where you'd need a virtual method).

When you access b2->x it knows that b2 points to a Base_2, and it
knows directly the offset of x.

--
__Pascal Bourguignon__
Jul 18 '08 #2
On Jul 18, 11:38 am, Rahul <rahulsha...@lucent.comwrote:
While reading Inside the C++ object model I came through the following
paragraph
Point3d origin, *ptr = &origin;
A) origin.x = 0.0;
B) ptr->x = 0.0;
The book says "A & B statements performs equivalently if x is
a member of a struct, class, single inheritance hierarchy, or
multiple inheritance hierarchy" This is because compiler knows
the offset of the member at compile time.
With one exception: if x is a member of a virtual base class of
Point3d. (And even then, in the above code, the compiler knows
that ptr points to a Point3d, and not a class derived from a
Point3d.)
My doubt is, How can the compiler know the offset of x in
case of B for multiple inheritance.
It knows how it laid out the object.
suppose we have
class Base_1{public: int i;}
class Base_2{public: int x;}
class Derived: public Base_1, public Base_2: {public: int k;}
now the offset of x will be different in Base_2 and Derived,
and the ptr may refer to any kind of object at run time,
No. ptr may only refer to a Point3d at run time. That Point3d
may be the base class of a more derived class, but that doesn't
change anything. The compiler knows where both i and x are in a
Point3d, and can generate the necessary code.
so how can we know the offset at compile time.
I mean the access through pointer must be slower in the above
case of Multiple inheritance. Please correct me if I am wrong.
Access through the pointer may be slower, because on some
machines, access through a pointer is slower than direct access.
(Of course, on other machines, such as the Sparc's I usuallly
work on, it is faster.) But other than that, there's no
difference as long as virtual inheritance is not involved.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Jul 18 '08 #3
On Jul 18, 1:38*pm, Rahul <rahulsha...@lucent.comwrote:
While reading Inside the C++ object model I came through the following
paragraph

Point3d origin, *ptr = &origin;
A) origin.x = 0.0;
B) ptr->x = 0.0;

The book says "A & B statements performs equivalently if x is a member
of a struct, class, single inheritance hierarchy, or multiple
inheritance hierarchy" This is because compiler knows the offset of
the member at compile time.

My doubt is, How can the *compiler know the offset of x in case of B
for multiple inheritance.
suppose we have
class Base_1{public: int i;}
class Base_2{public: int x;}
class Derived: public Base_1, public Base_2: {public: int k;}

now the offset of x will be different in Base_2 and Derived, and the
ptr may refer to any kind of object at run time, so how can we know
the offset at compile time.

I mean the access through pointer must be slower in the above case of
Multiple inheritance. Please correct me if I am wrong.
consider :

Derived d;
Base_2 *bptr=&d;

how do you think the second line should work?
It is equivalent to this:

Base_2 *bptr=static_cast<Base_2*>(&bptr);

Now the 'Base_2' subobject is availabe and everybody (including the
compiler) is happy with it.

cheers,
FM
Jul 19 '08 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Oystein Haare | last post by:
Is it best to declare class member objects as pointers or not pointers? class A { public: A(int i); //.... };
6
by: | last post by:
Say we have the following code defining TMyMsgHandler and TMyClass typedef void (*TOnMsgReceive) (TMyMessage Msg); class TMyMsgHandler { public: TMyMsgHandler(); virtual ~TMyMsgHandler();...
6
by: gustav04 | last post by:
hi all i have a question: what is the difference between a c-function and an c++ class method (both do exactly the same thing). lets say, i have a function called print2std() and a class...
6
by: Itay_k | last post by:
Hello, I want a member in my class that will save pointer to pointer to System::Drawing::Image class. When I write on my class code: System::Drawing::Image **bmp; I get this error message:...
7
by: _iycrd | last post by:
Is there an easy way to pin a pointer that is a member of a class, and leave that pointer pinned for the duration of the class's existence? The pointer would presumably be pinned inside the class's...
9
by: olanglois | last post by:
Hi, I am not sure if I have found a compiler bug (I am using VC++.NET2003) or if this is the correct behavior defined by the language but I am sure someone can clear up my confusion. Suppose the...
6
by: joosteto | last post by:
Subject: pointer to any member function of any class. The "C++ FAQ Lite" explains how to hand a pointer to a member function to a signal handler etc, but only to a static function (33.2), to a...
3
by: gyan | last post by:
Please go though following program: #include <iostream.h> class base { public: int basepublic; protected: int baseprotected; };
11
by: dalu.gelu | last post by:
Hi, can anyone help me by writing a sample code of defining a copy constructor in a class having data member as an object of another class. for eg: class A{ int x; public: A(){ x=6;} };
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

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.