C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Pointer to virtual object on stack

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++)
View previous topic :: View next topic  
Author Message
cyrusNew
Guest





PostPosted: Wed Dec 29, 2004 10:15 pm    Post subject: Pointer to virtual object on stack Reply with quote



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,


Back to top
Victor Bazarov
Guest





PostPosted: Wed Dec 29, 2004 10:33 pm    Post subject: Re: Pointer to virtual object on stack Reply with quote



cyrusNew wrote:
Quote:
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.

Quote:
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

Back to top
cyrusNew
Guest





PostPosted: Wed Dec 29, 2004 10:50 pm    Post subject: Re: Pointer to virtual object on stack Reply with quote




Uzytkownik "Victor Bazarov" <v.Abazarov (AT) comAcast (DOT) net> napisal w wiadomosci
news:NgGAd.34183$NC6.16450 (AT) newsread1 (DOT) mlpsca01.us.to.verio.net...
Quote:
cyrusNew wrote:
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

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.




Back to top
cpptutor2000@yahoo.com
Guest





PostPosted: Wed Dec 29, 2004 10:51 pm    Post subject: Re: Pointer to virtual object on stack Reply with quote

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"< };

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

Hope that helps.

cyrusNew wrote:
Quote:
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,


Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.