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

ATL布幔之下的秘密(2)

 2006-07-22 22:55:03 来源:WEB开发网   
核心提示: 程序24.#include <iostream>using namespace std;class Base {public:Base() {cout << "In Base" << endl;cout << "

程序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函数则使用了继承链中的最底部的派生类来创建对象。

上一页  1 2 3 4 5 6 7 8  下一页

Tags:ATL 之下 秘密

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接