设为首页 加入收藏

TOP

ATL布幔下的秘密之虚函数背后的东西(二)
2012-11-04 15:06:41 来源: 作者: 【 】 浏览:932
Tags:ATL 秘密 函数 背后 东西
  程序24.

#include <iostream>
using namespace std;

class Base {
 public:
 Base() {
  cout << "In Base" << endl;
  cout << "Virtual Pointer = " << (int*)this << endl;
  cout << "Address of Vtable = " << (int*)*(int*)this << endl;
  cout << "Value at Vtable = " << (int*)*(int*)*(int*)this << endl;
  cout << endl;
 }
 virtual void f1() { cout << "Base::f1" << endl; }
};

class Drive : public Base {
 public:
 Drive() {
  cout << "In Drive" << endl;
  cout << "Virtual Pointer = " << (int*)this << endl;
  cout << "Address of Vtable = " << (int*)*(int*)this << endl;
  cout << "Value at Vtable = " << (int*)*(int*)*(int*)this << endl;
  cout << endl;
 }
 virtual void f1() { cout << "Drive::f2" << endl; }
};

class MostDrive : public Drive {
 public:
 MostDrive() {
  cout << "In MostDrive" << endl;
  cout << "Virtual Pointer = " << (int*)this << endl;
  cout << "Address of Vtable = " << (int*)*(int*)this << endl;
  cout << "Value at Vtable = " << (int*)*(int*)*(int*)this << endl;
  cout << endl;
 }
 virtual void f1() { cout << "MostDrive::f2" << endl; }
};

int main() {
 MostDrive d;
 return 0;
}

  程序的输出为:

In Base
Virtual Pointer = 0012FF7C
Address of Vtable = 0046C0A0
Value at Vtable = 004010F5

In Drive
Virtual Pointer = 0012FF7C
Address of Vtable = 0046C090
Value at Vtable = 00401221

In MostDrive
Virtual Pointer = 0012FF7C
Address of Vtable = 0046C080
Value at Vtable = 00401186

  这个程序示范了虚函数表指针在每个类的构造函数中的初始化过程。这样看来,每个类构造函数中虚函数表的地址是不同的,main函数则使用了继承链中的最底部的派生类来创建对象。

  现在你可以看看虚函数表中各个类的构造函数的位置了,你可以将虚函数表中的第一个入口存入一个函数指针,并尝试运行它。

  程序25.

#include <iostream>
using namespace std;

typedef void(*Fun)();

class Base {
 public:
 Base() {
  cout << "In Base" << endl;
  cout << "Virtual Pointer = " << (int*)this << endl;
  cout << "Address of Vtable = " << (int*)*(int*)this << endl;
  cout << "Value at Vtable = " << (int*)*(int*)*(int*)this << endl;

  Fun pFun = (Fun)*(int*)*(int*)this;
  pFun();
  cout << endl;
 }
 virtual void f1() { cout << "Base::f1" << endl; }
};

class Drive : public Base {
 public:
 Drive() {
  cout << "In Drive" << endl;
  cout << "Virtual Pointer = " << (int*)this << endl;
  cout << "Address of Vtable = " << (int*)*(int*)this << endl;
  cout << "Value at Vtable = " << (int*)*(int*)*(int*)this << endl;

  Fun pFun = (Fun)*(int*)*(int*)this;
  pFun();
  cout << endl;
 }
 virtual void f1() { cout << "Drive::f1" << endl; }
};

class MostDrive : public Drive {
 public:
 MostDrive() {
  cout << "In MostDrive" << endl;
  cout << "Virtual Pointer = " << (int*)this << endl;
  cout << "Address of Vtable = " << (int*)*(int*)this << endl;
  cout << "Value at Vtable = " << (int*)*(int*)*(int*)this << endl;

  Fun pFun = (Fun)*(int*)*(int*)this;
  pFun();
  cout << endl;
 }
 virtual void f1() { cout << "MostDrive::f1" << endl; }
};

int main() {
 MostDrive d;
 return 0;
}

  程序的输出为:

In Base
Virtual Pointer = 0012FF7C
Address of Vtable = 0046C098
Value at Vtable = 004010F5
Base::f1

In Drive
Virtual Pointer = 0012FF7C
Address of Vtable = 0046C088
Value at Vtable = 00401221
Drive::f1

In MostDrive
Virtual Pointer = 0012FF7C
Address of Vtable = 0046C078
Value at Vtable = 00401186
MostDrive::f1

  这个程序示范了每个类中的构造函数是如何用自己的虚函数来填充虚函数表中的各入口的。所以,Base类使用Base类的虚函数地址来填充自己的虚函数表,当Drive类的构造函数执行它的时候会创建另外一个虚函数表,并存储自己的虚函数地址。

首页 上一页 1 2 3 4 5 下一页 尾页 2/5/5
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇ATL布幔下的秘密之模板技术 下一篇ATL布幔下的秘密之内部工作方式

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: