 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
cyrusNew Guest
|
Posted: Wed Dec 29, 2004 10:15 pm Post subject: 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,
|
|
| Back to top |
|
 |
Victor Bazarov Guest
|
Posted: Wed Dec 29, 2004 10:33 pm Post subject: Re: Pointer to virtual object on stack |
|
|
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
|
Posted: Wed Dec 29, 2004 10:50 pm Post subject: Re: Pointer to virtual object on stack |
|
|
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
|
Posted: Wed Dec 29, 2004 10:51 pm Post subject: 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"<
};
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 |
|
 |
|
|
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
|
|