开发学院软件开发VC ATL布幔之下的秘密(2) 阅读

ATL布幔之下的秘密(2)

 2006-07-22 22:55:03 来源:WEB开发网   
核心提示: 现在你可以看看虚函数表中各个类的构造函数的位置了,你可以将虚函数表中的第一个入口存入一个函数指针,ATL布幔之下的秘密(2)(4),并尝试运行它,程序25. #include <iostream>using namespace std;typedef void(*Fun)();

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

程序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 6 7 8 9  下一页

Tags:ATL 之下 秘密

编辑录入:爽爽 [复制链接] [打 印]
[]
  • 好
  • 好的评价 如果觉得好,就请您
      0%(0)
  • 差
  • 差的评价 如果觉得差,就请您
      0%(0)
赞助商链接