Pointer to virtual object on stack

C

cyrusNew

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,
 
V

Victor Bazarov

cyrusNew said:
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.

Do you really need to? Compile it and look at the assembly listing.
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.

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
 
C

cyrusNew

Uzytkownik "Victor Bazarov said:
Do you really need to? Compile it and look at the assembly listing.


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

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

cpptutor2000

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.
 

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,995
Messages
2,570,228
Members
46,818
Latest member
SapanaCarpetStudio

Latest Threads

Top