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