class member acces through pointer vs object

R

Rahul

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

Pascal J. Bourguignon

Rahul said:
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.
 
J

James Kanze

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.
 

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

No members online now.

Forum statistics

Threads
473,997
Messages
2,570,241
Members
46,831
Latest member
RusselWill

Latest Threads

Top