473,394 Members | 1,697 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,394 software developers and data experts.

calling virtual function from base class

"When you make a virtual function call through a base-class pointer
(that is, when you make a polymorphic call), the compiler quietly
inserts code to fetch the VPTR and look up the function address in the
VTABLE..."

Can anyone tell me how the VPTR of the derived class is fetched? Where
can I get more information bout this?

Thank u

Jul 22 '05 #1
4 1688

"Phui Hock" <bu****@gmail.com> wrote in message
news:ch********@odak26.prod.google.com...
"When you make a virtual function call through a base-class pointer
(that is, when you make a polymorphic call), the compiler quietly
inserts code to fetch the VPTR and look up the function address in the
VTABLE..."

Can anyone tell me how the VPTR of the derived class is fetched? Where
can I get more information bout this?

Thank u


'Inside the C++ Object Model' by Stanley Lippman.

john
Jul 22 '05 #2

"Phui Hock" <bu****@gmail.com> wrote in message
news:ch********@odak26.prod.google.com...
"When you make a virtual function call through a base-class pointer
(that is, when you make a polymorphic call), the compiler quietly
inserts code to fetch the VPTR and look up the function address in the
VTABLE..."

Can anyone tell me how the VPTR of the derived class is fetched? Where
can I get more information bout this?


The book John cited does indeed give useful insight into
the design of C++.

But note that the actual mechanics of e.g. polymorphism is
completely implementation-specific. Each might implement it
in its own way. The only language requirement is that a
particular *behavior* is the result. The language does not
define things such as 'v-tables' or 'v-pointers'.

-Mike
Jul 22 '05 #3
Phui Hock posted:
"When you make a virtual function call through a base-class pointer
(that is, when you make a polymorphic call), the compiler quietly
inserts code to fetch the VPTR and look up the function address in the
VTABLE..."

Can anyone tell me how the VPTR of the derived class is fetched? Where
can I get more information bout this?

Thank u


One thing I think you should realize: If you have a scenario where you have
a certain function:

void EatPoo()

And this function is NON-virtual in the base class:

class Base
{
public:

void EatPoo()
{
cout << "Base::EatPoo";
}
};

And then you have a derived class as so:

class Derived : public Base
{
public:

virtual void EatPoo()
{
cout << "Derived::EatPoo";
}
};

That now when you do the following:

int main()
{
Derived k;

Base& r = k;

r.EatPoo();
}

It's "Base::EatPoo" that will be called.
-JKop

Jul 22 '05 #4
Phui Hock wrote:
"When you make a virtual function call through a base-class pointer
(that is, when you make a polymorphic call),
the compiler quietly inserts code
to fetch the VPTR and look up the function address in the VTABLE..."

Can anyone tell me how the VPTR of the derived class is fetched?
Where can I get more information bout this?


The VPTR *is* a pointer to the VTABLE for the derived class
if the object was created using a constructor for the derived class.
Jul 22 '05 #5

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

Similar topics

2
by: William Payne | last post by:
Hello, consider these following two classes. A base class, class MDIChildWindow, and a class inherting from that base class, class Document. In the static base member function callback() I obtain a...
3
by: scott | last post by:
hi all, hope some one can help me. Ill try and explain what im trying to do as best i can. i have a parent class that has a vertual function, lets call it virtual int A(). That vertual function...
8
by: Andreas Lagemann | last post by:
Hi, after browsing FAQ and archive for a while I decided that the following is a legal question. Consider this: Class Base { public: Base() {}
11
by: santosh | last post by:
Hello, I was going through the Marshal Cline's C++ FAQ-Lite. I have a doubt regarding section 33.10. Here he is declaring a pure virtual destructor in the base class. And again defining...
8
by: Alex Vinokur | last post by:
Microsoft C++ Version 13.10.3077 Why is virtual function init() called in constructor here? ====== foo.cpp ====== #include "iostream" using namespace std; class Base {
12
by: Peter Cranz | last post by:
hello, I've got the following problem: I have a construct similar like this: namespace A { class X {
11
by: ypjofficial | last post by:
Hello All, So far I have been reading that in case of a polymorphic class ( having at least one virtual function in it), the virtual function call get resolved at run time and during that the...
3
by: Klaus | last post by:
Hi, I have an existing VC 6 MFC application which communicates asynchronly with a VC 2005 managed code dll. I use an unmanaged base class with virtual functions to access methods in the MFC...
7
by: =?ISO-8859-1?Q?Fernando_G=F3mez?= | last post by:
Hello all. I have this class with a virtual method and a constructor that calls this virtual method. A derived class overrides this virtual method, so I expected that when the base's constructor is...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.