473,379 Members | 1,255 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,379 software developers and data experts.

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,
Jul 22 '05 #1
3 1150
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
Jul 22 '05 #2

Uzytkownik "Victor Bazarov" <v.********@comAcast.net> napisal w wiadomosci
news:Ng*******************@newsread1.mlpsca01.us.t o.verio.net...
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.

Jul 22 '05 #3
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:
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,


Jul 22 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

37
by: Ben | last post by:
Hi, there. Recently I was working on a problem where we want to save generic closures in a data structure (a vector). The closure should work for any data type and any method with pre-defined...
38
by: Radde | last post by:
HI all, Whats the difference b/w pass by ref and pass by pointer in C++ when ur passing objects as args.. Cheers..
5
by: Scott | last post by:
Hi All, Am I correct in assuming that there is no way to have a base pointer to an object that uses multiple inheritance? For example, class A { /* ... */ }; class B { /* ... */ };
3
by: Nindi73 | last post by:
Hi, I am in need of a deep copy smart pointer (Boost doesn't provide one) which doesnt require the contained types to have a virtual copy constructor. I wrote a smart pointer class that I think...
18
by: happyvalley | last post by:
Hi, basically, the test function get a char pointer, and assigned a string to it. then the string is passed back by the call-by-reference mechanism. in test(), I reallocate some memory for the...
16
by: Alex Vinokur | last post by:
Does it have to be? : sizeof (size_t) >= sizeof (pointer) Alex Vinokur email: alex DOT vinokur AT gmail DOT com http://mathforum.org/library/view/10978.html http://sourceforge.net/users/alexvn
9
by: Morten Lemvigh | last post by:
Is it possible to pass a pointer to a constructor or a class definition as argument to a function? Maybe in a way similar to passing function pointers...? The function should construct a number...
5
by: Tim Frink | last post by:
Hi, I'm experimenting with function pointers and found two questions. Let's assume this code: 1 #include <iostream> 2 class A; 3 4 //////////////////////////////////////////// 5 class B
20
by: MikeC | last post by:
Folks, I've been playing with C programs for 25 years (not professionally - self-taught), and although I've used function pointers before, I've never got my head around them enough to be able to...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.