__Virtual function and their implementation

原创 2007年10月01日 23:12:00
 C++ virtual function is ,
       1. a member function of a class
       2. Declare with keyword virtual
       3. Usually has different functionality in derived class
       4. A function call is revolved at run-time.
#include <iostream>
using namespace std;
class Base{
public:
    virtual _cdecl fun(int q){
       cout<<"Hello, base:"<<endl<<q<<endl;
    }
    void CallVirtualFnUsingThis()
    {
       int *p = (int*)this;
       cout<<"Address of the object:"<<hex<<*p<<endl;
       p = (int*)*p;
       cout<<"Address of VPTR:"<<hex<<*p<<endl;
       p = (int*)*p;
       cout<<"address of First function in Vitual table:"<<hex<<*p<<endl;
       void (_cdecl *pFun)(Base* const, int);
       pFun = (void(_cdecl*)(Base* const, int))p;
       (*pFun)(this, 0x25);
    }
};
int main()
{
    Base b;
    b.CallVirtualFnUsingThis();
 return 0;
}

当基类中含有虚函数时, 编译器会自动进行以下的动作:

 

1、为每一个类增加一个VTABLE VTABLE里记录了各各类里面, 所有在类或基类内声明为virtual的成员函数地址(代码所在地址)。

2、对基类和每个派生类都加入一个VPTR,并对所有这些类所定义对象内的VPTR进行初始化操作:使VPTR指向正确的VTABLE的第一个字段; VPTR是位于对象层次上的概念,当他指向了一个VTABLE时, 就知道自己是什么类型了。。

3、对每一个虚函数调用,都加入专用的虚函数代码,获取正确的函数地址。

 

the typical compiler creates a single table (called the VTABLE) for each class that contains virtual functions. The compiler places the addresses of the virtual functions for that particular class in the VTABLE. In each class with virtual functions, it secretly places a pointer, called the vpointer (abbreviated as VPTR), which points to the VTABLE for that object. 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, thus calling the correct function and causing late binding to take place.

All of this – setting up the VTABLE for each class, initializing the VPTR, inserting the code for the virtual function call – happens automatically, so you don’t have to worry about it. With virtual functions, the proper function gets called for an object, even if the compiler cannot know the specific type of the object.

相关文章推荐

tn nike pas cher and they are the same age fellow worthy of their admiration

some of whom represent their own business. Meanwhile, the reporter received a China Soong Ching Ling...

WORDS AND THEIR STORIES - A Wolf in Sheep's Clothing(披着羊皮的狼)

Last week, I explained some English expressions about clothes.  Everything I told you was true.  I...
  • omg2012
  • omg2012
  • 2011年09月01日 13:25
  • 187

textons,the elements of texture perception and their interactions_julesz

前意识(pre-attentive),指无意识中可召回的部分,人们能够回忆起来的经验。它是无意识和意识之间的中介环节。无意识很难或根本不能进入意识,前意识则可能进入意识,所以从前意识到意识尽管有界限,...

Distributed Representations of Words and Phrasesand their Compositionality

https://papers.nips.cc/paper/5021-distributed-representations-of-words-and-phrases-and-their-composi...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:__Virtual function and their implementation
举报原因:
原因补充:

(最多只允许输入30个字)