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

ATL布幔之下的秘密(2)

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

程序32. #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 1st entry = " << (int*)*((int*)*(int*)this+0) << endl;
    cout << "Value at Vtable 2nd entry = " << (int*)*((int*)*(int*)this+1) << endl;
    cout << endl;
  }
  virtual void f1() = 0;
  virtual void f2() = 0;
};
class __declspec(novtable) 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 1st entry = " << (int*)*((int*)*(int*)this+0) << endl;
    cout << "Value at Vtable 2nd entry = " << (int*)*((int*)*(int*)this+1) << endl;
    cout << endl;
  }
};
class __declspec(novtable) 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 1st entry = " << (int*)*((int*)*(int*)this+0) << endl;
    cout << "Value at Vtable 2nd entry = " << (int*)*((int*)*(int*)this+1) << endl;
    cout << endl;
  }
  virtual void f1() { cout << "MostDrive::f1" << endl; }
  virtual void f2() { cout << "MostDrive::f2" << endl; }
};
int main() {
  MostDrive d;
  return 0;
}
  
现在,程序的输出为:In Base
Virtual Pointer = 0012FF7C
Address of Vtable = 0046C0C0
Value at Vtable 1st entry = 00420E50
Value at Vtable 2nd entry = 00420E50
In Drive
Virtual Pointer = 0012FF7C
Address of Vtable = 0046C0C0
Value at Vtable 1st entry = 00420E50
Value at Vtable 2nd entry = 00420E50
In MostDrive
Virtual Pointer = 0012FF7C
Address of Vtable = 0046C0C0
Value at Vtable 1st entry = 00420E50
Value at Vtable 2nd entry = 00420E50
在MSDN中,对__declspec(novtable)的解释是:它应该使用在纯虚类中。那么,让我们再做一个实验来更好地弄懂这一含义吧。

上一页  5 6 7 8 9 10 

Tags:ATL 之下 秘密

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