程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> C++ virtual table pointer - vptr

C++ virtual table pointer - vptr

編輯:C++入門知識

C++ virtual table pointer - vptr


To implement virtual functions, C++ uses a special form of late binding known as the virtual table. The virtual table is a lookup table of functions used to resolve function calls in a dynamic/late binding manner. The virtual table sometimes goes by other names, such as “vtable”, “virtual function table”, “virtual method table”, or “dispatch table”.

Because knowing how the virtual table works is not necessary to use virtual functions, this section can be considered optional reading.

The virtual table is actually quite simple, though it’s a little complex to describe in words. First, every class that uses virtual functions (or is derived from a class that uses virtual functions) is given it’s own virtual table. This table is simply a static array that the compiler sets up at compile time. A virtual table contains one entry for each virtual function that can be called by objects of the class. Each entry in this table is simply a function pointer that points to the most-derived function accessible by that class.

Second, the compiler also adds a hidden pointer to the base class, which we will call *__vptr. *__vptr is set (automatically) when a class instance is created so that it points to the virtual table for that class. Unlike the *this pointer, which is actually a function parameter used by the compiler to resolve self-references, *__vptr is a real pointer. Consequently, it makes each class object allocated bigger by the size of one pointer. It also means that *__vptr is inherited by derived classes, which is important.

By now, you’re probably confused as to how these things all fit together, so let’s take a look at a simple example:
view source
print?
01 class Base
02 {
03 public:
04 virtual void function1() {};
05 virtual void function2() {};
06 };
07
08 class D1: public Base
09 {
10 public:
11 virtual void function1() {};
12 };
13
14 class D2: public Base
15 {
16 public:
17 virtual void function2() {};
18 };

Because there are 3 classes here, the compiler will set up 3 virtual tables: one for Base, one for D1, and one for D2.

The compiler also adds a hidden pointer to the most base class that uses virtual functions. Although the compiler does this automatically, we’ll put it in the next example just to show where it’s added:
view source
print?
01 class Base
02 {
03 public:
04 //A *__vptr is always here;
05 virtual void function1() {};
06 virtual void function2() {};
07 };
08
09 class D1: public Base
10 {
11 public:
12 virtual void function1() {};
13 };
14
15 class D2: public Base
16 {
17 public:
18 virtual void function2() {};
19 };

When a class object is created, *__vptr is set to point to the virtual table for that class. For example, when a object of type Base is created, *__vptr is set to point to the virtual table for Base. When objects of type D1 or D2 are constructed, *__vptr is set to point to the virtual table for D1 or D2 respectively.

Now, let’s talk about how these virtual tables are filled out. Because there are only two virtual functions here, each virtual table will have two entries (one for function1(), and one for function2()). Remember that when these virtual tables are filled out, each entry is filled out with the most-derived function an object of that class type can call.

Base’s virtual table is simple. An object of type Base can only access the members of Base. Base has no access to D1 or D2 functions. Consequently, the entry for function1 points to Base::function1(), and the entry for function2 points to Base::function2().

D1’s virtual table is slightly more complex. An object of type D1 can access members of both D1 and Base. However, D1 has overridden function1(), making D1::function1() more derived than Base::function1(). Consequently, the entry for function1 points to D1::function1(). D1 hasn’t overridden function2(), so the entry for function2 will point to Base::function2().

D2’s virtual table is similar to D1, except the entry for function1 points to Base::function1(), and the entry for function2 points to D2::function2().

Here’s a picture of this graphically:

\

Although this diagram is kind of crazy looking, it’s really quite simple: the *__vptr in each class points to the virtual table fZ喎?http://www.Bkjia.com/kf/ware/vc/" target="_blank" class="keylink">vciB0aGF0IGNsYXNzLiBUaGUgZW50cmllcyBpbiB0aGUgdmlydHVhbCB0YWJsZSBwb2ludCB0byB0aGUgbW9zdC1kZXJpdmVkIHZlcnNpb24gb2YgdGhlIGZ1bmN0aW9uIG9iamVjdHMgb2YgdGhhdCBjbGFzcyBhcmUgYWxsb3dlZAogdG8gY2FsbC48YnI+Cjxicj4KU28gY29uc2lkZXIgd2hhdCBoYXBwZW5zIHdoZW4gd2UgY3JlYXRlIGFuIG9iamVjdCBvZiB0eXBlIEQxOjxicj4KdmlldyBzb3VyY2U8YnI+CnByaW50Pzxicj4KMSAgICAgaW50IG1haW4oKTxicj4KMiAgICAgezxicj4KMyAgICAgICAgIEQxIGQxOzxicj4KNCAgICAgfTxicj4KPGJyPgpCZWNhdXNlIGQxIGlzIGEgRDEgb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yBhIHJlc3VsdCwgd2UgaGF2ZSB0byBkbyAzIG9wZXJhdGlvbnMgdG8gZmluZCB0aGUgZnVuY3Rpb24gdG8gY2FsbCwgYXMgb3Bwb3NlZCB0byAyIG9wZXJhdGlvbnMgZm9yIGEgbm9ybWFsIGluZGlyZWN0IGZ1bmN0aW9uIGNhbGwsIG9yIG9uZSBvcGVyYXRpb24gZm9yIGEgZGlyZWN0IGZ1bmN0aW9uIGNhbGwuIEhvd2V2ZXIsIHdpdGggbW9kZXJuIGNvbXB1dGVycywgdGhpcyBhZGRlZAogdGltZSBpcyB1c3VhbGx5IGZhaXJseSBpbnNpZ25pZmljYW50LjwvcD4KPHA+PGJyPgo8L3A+CjxwPkJ5IHRoZSB3YXksIGhlcmU="s an example of mine

#if 01
//vptr
#include 
using namespace std;

class Base
 {
public:
  int a;
  int b;
  Base(int temp1 = 0, int temp2 = 0)
  {
	 a=temp1;
	 b=temp2; 
  }
  virtual ~Base(){std::cout << "~Base()" << std::endl;};

  virtual int getA()  
  {
	 return a;
  }
  virtual int getB()  
  {
	 return b;
  }
};

class Derive: public Base
{
public:
  // *__vptr is inherited by derived classes
  Derive(int d1, int d2):Base(d1,d2){}
  virtual int getA(){return a;}
  virtual int getB(){return b;} 
};

class Empty{
public:
  int e;
};

int main() 
{
  Derive obj(5, 10);
  std::cout << "sizeof class: " << sizeof(Base) << std::endl;

  std::cout << "the &obj :  " << &obj << std::endl;//&obj = the hidden pointer, which we will call *__vptr. *
  std::cout << "the &obj+1: " << &obj + 1 << std::endl;//As sizeof(test) is 16, so  (Base *) + 1 means addr + 16
  std::cout << "the &obj.a: " << &obj.a << std::endl;
  std::cout << "the &obj.b: " << &obj.b << std::endl;
  // Changing a and b by *__vptr
  std::cout << "--------------Set member variable over vptr(8 bytes = 2*sizeof(int), so &a = pInt + 2)--------------" << std::endl;
  int* pInt = (int*)&obj;
  *(pInt+2) = 100;   
  *(pInt+3) = 200;   

  cout << "a = " << obj.getA() << endl;
  cout << "b = " << obj.getB() << endl;
  
  Empty e;
  std::cout << "the &e: " << &e << std::endl;
  std::cout << "the &e.e: " << &e.e << std::endl;//&e = &e.e, so there's no virtual table pointer
  return 0;
}
#endif


  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved