Pointer to virtual object on stack

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

    #1

    Pointer to virtual object on stack

    Hi,

    I need help to resolve confusion on following example :

    class A {
    public:
    virtual void GetName() = 0;
    };

    class B : public A {
    public:
    void GetName() { cout << "B"; }
    };

    B b;
    A* pA = &b;
    pA->GetName();

    I think this is correct code, but I dont quite understand whats happening
    behind it. I mean is pointer pA just equal to B's vptr and if so then does
    it require some special sorting of elements of vtbl, like first comes A
    virtual methods then B's virtual methods.

    Thanks,


  • Victor Bazarov

    #2
    Re: Pointer to virtual object on stack

    cyrusNew wrote:[color=blue]
    > I need help to resolve confusion on following example :
    >
    > class A {
    > public:
    > virtual void GetName() = 0;
    > };
    >
    > class B : public A {
    > public:
    > void GetName() { cout << "B"; }
    > };
    >
    > B b;
    > A* pA = &b;
    > pA->GetName();
    >
    > I think this is correct code, but I dont quite understand whats happening
    > behind it.[/color]

    Do you really need to? Compile it and look at the assembly listing.
    [color=blue]
    > I mean is pointer pA just equal to B's vptr and if so then does
    > it require some special sorting of elements of vtbl, like first comes A
    > virtual methods then B's virtual methods.[/color]

    pA just points to the A subobject of 'b'. They share the vtbl. Whenever
    a virtual function call is to be made, it's resolved by indirection with
    a corresponding entry in the vtbl, the compiler takes care of that.

    When 'b' is fully constructed, the vtbl contains addresses of all 'A'
    functions that are not overridden by 'B' and those that override 'A's
    ones. Until 'b' is fully constructed, the vtbl probably contains only
    the addresses of 'A's functions.

    This is not really defined by the language, BTW. So, whatever ideas you
    get about the implementation of the virtual functions mechanism, it does
    not have to be the same everywhere.

    V

    Comment

    • cyrusNew

      #3
      Re: Pointer to virtual object on stack


      Uzytkownik "Victor Bazarov" <v.Abazarov@com Acast.net> napisal w wiadomosci
      news:NgGAd.3418 3$NC6.16450@new sread1.mlpsca01 .us.to.verio.ne t...[color=blue]
      > cyrusNew wrote:[color=green]
      >> I need help to resolve confusion on following example :
      >>
      >> class A {
      >> public:
      >> virtual void GetName() = 0;
      >> };
      >>
      >> class B : public A {
      >> public:
      >> void GetName() { cout << "B"; }
      >> };
      >>
      >> B b;
      >> A* pA = &b;
      >> pA->GetName();
      >>
      >> I think this is correct code, but I dont quite understand whats happening
      >> behind it.[/color]
      >
      > Do you really need to? Compile it and look at the assembly listing.
      >[color=green]
      > > I mean is pointer pA just equal to B's vptr and if so then does
      >> it require some special sorting of elements of vtbl, like first comes A
      >> virtual methods then B's virtual methods.[/color]
      >
      > pA just points to the A subobject of 'b'. They share the vtbl. Whenever
      > a virtual function call is to be made, it's resolved by indirection with
      > a corresponding entry in the vtbl, the compiler takes care of that.
      >
      > When 'b' is fully constructed, the vtbl contains addresses of all 'A'
      > functions that are not overridden by 'B' and those that override 'A's
      > ones. Until 'b' is fully constructed, the vtbl probably contains only
      > the addresses of 'A's functions.
      >
      > This is not really defined by the language, BTW. So, whatever ideas you
      > get about the implementation of the virtual functions mechanism, it does
      > not have to be the same everywhere.
      >
      > V[/color]

      Thank you for answer, I just started using vectors like vector<B> and
      getting pointers of its elements of type A*. Until now I was mostly using
      dynamic allocation for creation of objects of polymorphic types so I wanted
      to be clear if all is working correctly. Now I will just look into "Inside
      the C++ Object Model" by Stanley Lippman for further info.



      Comment

      • cpptutor2000@yahoo.com

        #4
        Re: Pointer to virtual object on stack

        It is a very simple case of inheritance and dynamic binding. Class B is

        derived from clas A, which means that B 'isa' A, anytime an instance of
        B is generated, an instance of A has to be. The pure virtual function
        of A is
        being overridden in B. By inheritance, the statement 'A* pA = &b;' is
        perfectly
        valid, and when you have 'pA->GetName();' the dynamic binding uses B's
        implementation of 'GetName()'. Try out the following:

        #include <iostream>

        using namespace std;

        class A{
        public:
        A(){}
        virtual void doit() = 0;
        };

        class B : public A {
        public:
        B():A(){}
        ~B(){}
        void doit(){ cout<<" This is B"<<endl; }
        };

        int main(){
        B b;
        A *ap = &b;
        ap->doit();
        return 0;
        }

        Hope that helps.

        cyrusNew wrote:[color=blue]
        > Hi,
        >
        > I need help to resolve confusion on following example :
        >
        > class A {
        > public:
        > virtual void GetName() = 0;
        > };
        >
        > class B : public A {
        > public:
        > void GetName() { cout << "B"; }
        > };
        >
        > B b;
        > A* pA = &b;
        > pA->GetName();
        >
        > I think this is correct code, but I dont quite understand whats[/color]
        happening[color=blue]
        > behind it. I mean is pointer pA just equal to B's vptr and if so then[/color]
        does[color=blue]
        > it require some special sorting of elements of vtbl, like first comes[/color]
        A[color=blue]
        > virtual methods then B's virtual methods.
        >
        > Thanks,[/color]

        Comment

        Working...