class member acces through pointer vs object

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Rahul

    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.
  • Pascal J. Bourguignon

    #2
    Re: class member acces through pointer vs object

    Rahul <rahulsharma@lu cent.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__

    Comment

    • James Kanze

      #3
      Re: class member acces through pointer vs object

      On Jul 18, 11:38 am, Rahul <rahulsha...@lu cent.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:james.kan ze@gmail.com
      Conseils en informatique orientée objet/
      Beratung in objektorientier ter Datenverarbeitu ng
      9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

      Comment

      • terminator

        #4
        Re: class member acces through pointer vs object

        On Jul 18, 1:38 pm, Rahul <rahulsha...@lu cent.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_ca st<Base_2*>(&bp tr);

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

        cheers,
        FM

        Comment

        Working...