ATL布幔之下的秘密(2)

七星棋牌顶级运营产品全开源修复版源码教程:6端支持,200+子游戏玩法,完整搭建指南(含代码解析) 让您从源码下载到本地运行一步到位!此外,文中还将附带详细代码解析与操作步骤,即便没有开发经验,也能轻松完成二开与定制。数据库是棋牌游戏中极为关键的组成部分,用于存储用户信息、乐豆余额、游戏记录、提现申请等数据。以下是部分核心数据表结构示例,便于存储用户信息与游戏记录。AI模块能够控制游戏中的胜率,常用于调节玩家体验。防沉迷功能旨在限制未成年人游戏时间。的完整搭建流程,特别适合。首先,获取七星棋牌源码。本教程将为您详细讲解。 阅读详情

原作者:Zeeshan Amjad
原文链接:http://www.codeproject.com/atl/atl_underthehood_2.asp

介绍
  在本系列的教程中,我要讨论一些ATL的内部工作方式以及它所使用的技术,这是本系列的第二篇文章。

  现在让我们来探究一些虚函数背后更加有趣的资料。为了和上文保持一致,在本文的讨论中我将使用相同的顺序,程序的序号从20开始。
  让我们看看下面这个程序:
程序20.
#include <iostream>
using namespace std;

class Base {
public:
    virtual void fun() {
        cout << "Base::fun" << endl;
    }
    void show() {
        fun();
    }
};

class Drive : public Base {
public:
    virtual void fun() {
        cout << "Drive::fun" << endl;
    }
};

int main() {
    Drive d;
    d.show();

    return 0;
}
  程序的输出为:
Drive::fun

  这个程序清楚地示范了基类的函数是如何调用派生类的虚函数的。这一技术被用于不同的框架中,例如MFC和设计模式(比如Template Design Pattern)。现在你可以修改一下这个程序来看看它的行为,我将要在基类的构造函数中调用虚函数,而不是普通的成员函数。
程序21.
#include <iostream>
using namespace std;

class Base {
public:
    Base() {
        fun();
    }
    virtual void fun() {
        cout << "Base::fun" << endl;
    }
};

class Drive : public Base {
public:
    virtual void fun() {
        cout << "Drive::fun" << endl;
    }
};

int main() {
    Drive d;

    return 0;
}
  程序的输出为:
Base::fun
  这个程序表明,我们不能在基类的构造函数中调用派生类的虚函数。好了,那就让我们来看看着布幔之下到底做了什么。我将会把这些构造函数之中的指针值打印出来,为了简便起见,我移除了类中其它的函数。
程序22.
#include <iostream>
using namespace std;

class Base {
public:
    Base() {
        cout << "In Base" << endl;
        cout << "This Pointer = " << (int*)this << endl;
        cout << endl;
    }
virtual void f() { cout << "Base::f" << endl; }
};

class Drive : public Base {
public:
    Drive() {
        cout << "In Drive" << endl;
        cout << "This Pointer = " << (int*)this << endl;
        cout << endl;
    }
virtual void f() { cout << "Drive::f" << endl; }
};

int main() {
    Drive d;
    cout << "In Main" << endl;
    cout << (int*)&d << endl;

    return 0;
}
  程序的输出为:
In Base
This Pointer = 0012FF7C

In Drive
This Pointer = 0012FF7C

In Main
0012FF7C

  这就表示,整个内存位置中,只有一个对象的存在。那么就让我们把这个指针指向的值打印出来,也就是虚函数表的指针vptr指向的值,VTable的地址。
程序23.
#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; }
};

int main() {
    Drive d;
    return 0;
}
  程序的输出为:
In Base
Virtual Pointer = 0012FF7C
Address of Vtable = 0046C08C
Value at Vtable = 004010F0

In Drive
Virtual Pointer = 0012FF7C
Address of Vtable = 0046C07C
Value at Vtable = 00401217

  这个程序示范了基类和派生类中不同的虚函数表地址。为了更好地弄懂这一问题,那就让我们把继承层次加深,并添加一个继承于Drive类的MostDrive类,然后构建一个它的对象。
程序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类的构造函数执行它的时候会创建另外一个虚函数表,并存储自己的虚函数地址。
  现在,你会发现在基类中含有多个虚函数的情况下,派生类并不能完全重写它们。
程序26.
#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 << "Value at Vtable 3rd entry = " << (int*)*((int*)*(int*)this+2) << endl;
cout << endl;
    }
    virtual void f1() { cout << "Base::f1" << endl; }
    virtual void f2() { cout << "Base::f2" << 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 1st entry = " << (int*)*((int*)*(int*)this+0) << endl;
        cout << "Value at Vtable 2nd entry = " << (int*)*((int*)*(int*)this+1) << endl;
        cout << "Value at Vtable 3rd entry = " << (int*)*((int*)*(int*)this+2) << endl;
        cout << endl;
    }
    virtual void f1() { cout << "Drive::f1" << endl; }
};

int main() {
    Drive d;
    return 0;
}
  程序的输出为:
In Base
Virtual Pointer = 0012FF7C
Address of Vtable = 0046C0E0
Value at Vtable 1st entry = 004010F0
Value at Vtable 2nd entry = 00401145
Value at Vtable 3rd entry = 00000000

In Drive
Virtual Pointer = 0012FF7C
Address of Vtable = 0046C0C8
Value at Vtable 1st entry = 0040121C
Value at Vtable 2nd entry = 00401145
Value at Vtable 3rd entry = 00000000

  这个程序的输出表明基类的虚函数在派生类中并未被重写,然后,派生类的构造函数没有对虚函数的入口做任何的事情。
  那么现在,让我邀请纯虚函数来加入这一游戏,再来看看它的行为吧。请看以下的程序:
程序27.
#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 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;
    }
    virtual void f1() { cout << "Drive::f1" << endl; }
    virtual void f2() { cout << "Drive::f2" << endl; }
};

int main() {
    Drive d;

    return 0;
}

  在debug和release模式下,程序的输出有所不同。下面是debug模式的输出:
In Base
Virtual Pointer = 0012FF7C
Address of Vtable = 0046C0BC
Value at Vtable 1st entry = 00420CB0
Value at Vtable 2nd entry = 00420CB0

In Drive
Virtual Pointer = 0012FF7C
Address of Vtable = 0046C0A4
Value at Vtable 1st entry = 00401212
Value at Vtable 2nd entry = 0040128F

  以下则是release模式的输出:
In Base
Virtual Pointer = 0012FF80
Address of Vtable = 0042115C
Value at Vtable 1st entry = 0041245D
Value at Vtable 2nd entry = 0041245D

In Drive
Virtual Pointer = 0012FF80
Address of Vtable = 00421154
Value at Vtable 1st entry = 00401310
Value at Vtable 2nd entry = 00401380

  为了更好地弄懂这一原理,我们需要对程序作少许的改动,并尝试使用函数指针来调用虚函数。
程序28.
#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 1st entry = " << (int*)*((int*)*(int*)this+0) << endl;
        cout << "Value at Vtable 2nd entry = " << (int*)*((int*)*(int*)this+1) << endl;
        
        // 尝试执行第一个虚函数
        Fun pFun = (Fun)*((int*)*(int*)this+0);
        pFun();

        cout << endl;
    }
    virtual void f1() = 0;
    virtual void f2() = 0;
};

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 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 << "Drive::f1" << endl; }
    virtual void f2() { cout << "Drive::f2" << endl; }
};

int main() {
    Drive d;

    return 0;
}

  现在程序的行为在debug和release模式下仍然不同。在debug模式下,它会显示一个运行时错误的对话框:

  并且,当你按下“忽略”按钮之后,它会显示下面的对话框:

  而在release模式下运行的话,它只会在控制台窗口中输出错误信息:
In Base
Virtual Pointer = 0012FF80
Address of Vtable = 0042115C
Value at Vtable 1st entry = 0041245D
Value at Vtable 2nd entry = 0041245D

runtime error R6025
- pure virtual function call

  那么这里的R6025是什么?它定义于CMSGS.H头文件中,这个头文件定义了所有C Run Time Library的所有错误信息。
#define _RT_PUREVIRT_TXT   "R6025" EOL "- pure virtual function call" EOL
  事实上,当我们定义了纯虚函数后,编译器就会放置一个名为_purecall的C Run Time Library的函数地址。这个函数定义在PUREVIRT.C之中,它的原型如下:
void __cdecl _purecall(void); // 译注:原文此处无分号
  我们可以在程序中直接调用这个函数来达到相同的效果,请看下面这个小程序:
程序29.
int main() {
    _purecall();    
    return 0;
}

  这个程序在debug模式和release模式下的输出和前一个是一样的。为了更好的理解这个问题,让我们把继承链弄得更深一些,并且从Drive类中再继承一个类来看看效果吧。
程序30.
#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 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 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 = 0046C0D8
Value at Vtable 1st entry = 00420F40
Value at Vtable 2nd entry = 00420F40

In Drive
Virtual Pointer = 0012FF7C
Address of Vtable = 0046C0C0
Value at Vtable 1st entry = 00420F40
Value at Vtable 2nd entry = 00420F40

In MostDrive
Virtual Pointer = 0012FF7C
Address of Vtable = 0046C0A8
Value at Vtable 1st entry = 00401186
Value at Vtable 2nd entry = 004010F5

  这个程序表明,Base和Drive类是用相同的值来初始化各自的虚函数表的。那么,如果继承更深一些,并且只有最底层的派生类重写了纯虚函数,在这种情况下又会发生什么呢?这就是在COM程序设计的情况下所发生的了——接口就是只拥有纯虚函数的类,并且一个接口是继承自另一个接口的,只有实现类才会重写接口的纯虚函数。这样一来,每个基类的构造函数就会以相同的值来初始化它们自己的虚函数表入口。所以,这就意味着相同的代码会反复重复下去。
  ATL的主要思想就是让COM组件尽可能的小,但是由于这一行为,接口类的构造函数就会拥有很多不必要的代码。为了解决这一问题,ATL引入了一个宏ATL_NO_VTABLE,它定义在ATLDEF.H中:
#define ATL_NO_VTABLE __declspec(novtable)
  __declspec(novtable)为Microsoft C++扩展的类属性。它会使编译器不产生初始化虚函数表指针和虚函数表的代码,这样一来就减少了生成代码的尺寸。
  那么,我们来修改一下我们的代码,这样就能更好的明白这一属性究竟能为我们做什么了。
程序31.
#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 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 = 0046C0CC
Value at Vtable 1st entry = 00420E60
Value at Vtable 2nd entry = 00420E60

In Drive
Virtual Pointer = 0012FF7C
Address of Vtable = 0046C0B4
Value at Vtable 1st entry = 00420E60
Value at Vtable 2nd entry = 00420E60

In MostDrive
Virtual Pointer = 0012FF7C
Address of Vtable = 0046C0B4
Value at Vtable 1st entry = 00420E60
Value at Vtable 2nd entry = 00420E60
  这个程序有另外一个结果,也就是Drive和MostDrive类拥有相同的虚函数表地址,但是Base类却不同。事实上,这就是由于我们没有对Base类使用__declspec(novtable)属性的缘故。现在,我们来对继承的Drive类也使用相同的属性,也就是__declspec(novtable)。
程序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)的解释是:它应该使用在纯虚类中。那么,让我们再做一个实验来更好地弄懂这一含义吧。
程序33.
#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;

        // 尝试调用第一个虚函数
        typedef void (*Fun)();
        Fun pFun = (Fun)*((int*)*(int*)this+0);
        pFun();

    }
    virtual void f1() { cout << "MostDrive::f1" << endl; }
    virtual void f2() { cout << "MostDrive::f2" << endl; }
};

int main() {
    MostDrive d;

    return 0;
}
  我们在程序中添加的新东西是:
// 尝试调用第一个虚函数
typedef void (*Fun)();
Fun pFun = (Fun)*((int*)*(int*)this+0);
pFun();

  并且,当我们运行这个应用程序的时候,我们会面对与前一个程序相同的问题——也就是尝试调用虚函数发生的错误。这就意味着虚函数表并未初始化。MostDrive类并不是一个抽象类,所以我们应该从类中移除__declspec(novtable)。
程序34.
#include <iostream>
using namespace std;

class Base {
public:
    virtual void f1() = 0;
    virtual void f2() = 0;
};

class __declspec(novtable) Drive : public Base {
};

class MostDrive : public Drive {
public:
    MostDrive() {

        // 尝试调用第一个虚函数
        typedef void (*Fun)();
        Fun pFun = (Fun)*((int*)*(int*)this+0);
        pFun();

    }
    virtual void f1() { cout << "MostDrive::f1" << endl; }
    virtual void f2() { cout << "MostDrive::f2" << endl; }
};

int main() {
    MostDrive d;

    return 0;
}
  现在,程序就可以正常工作了。它的输出为:
MostDrive::f1

  这个属性并不一定只用在ATL的类中,它可以对任何不能创建对象的类使用。同样,它也并不一定非要用在ATL类中,也就是说它可以从ATL类中省略,不过这就意味着这样会产生更多的代码。
  我希望能在下篇文章中探究更多ATL的秘密。

设计四翼无人机(Quadcopter)的自主降落控制算法 为四翼无人机建立动力学模型,包括其姿态(Pitch, Roll, Yaw)和位置(X, Y, Z)的变化。可以使用Aerospace Blockset中的预制模块,或者根据具体需求构建自定义模型。示例:简化动力学模型利用块模拟四翼无人机的基本行为,确保模型能够反映电机输出如何影响姿态和位置变化。 阅读详情

相关推荐

C#实战:如何用Bartender 10.1实现动态打印与批量打印(附完整源码)

本文深入探讨了如何在C#项目中高效集成Bartender 10.1,实现企业级的动态打印与批量打印功能。文章提供了从环境配置、COM接口精讲、高可用打印服务类封装,到批量打印性能优化及生产环境部署的全套实战方案与源码,帮助开发者构建健壮、可维护的打印解决方案。

weixin_29290963的博客 443

ATL炒冷饭学习之二:绕不开的虚函数

ATL炒冷饭学习之二:绕不开的虚函数 一、前言 对于C++的程序员,多态机制是再熟悉不过的了;多态就是用父类型别的指针指向其子类的实例,然后通过父类的指针调用实际子类的成员函数。对于多态机制是如何实现的原理,想必对于一个c++程序员来说是小菜一碟。对于即将学习COM技术的人,将会明白com接口有将这一机制运用到了极致,所以,多态技术也是学习COM技术的前提和基础,不知道多态机制的人,是永运无法学习明白COM的。所以,在开始学习COM时,是非常有必要专门复习一下C++的多态机制是如何实现的。 ...

jyl_sh的专栏 1159

IEEE论文投稿必看:PDF eXpress报错font not embedded的3种修复方法(附Adobe Acrobat DC操作截图)

本文针对IEEE论文投稿时PDF eXpress系统常见的“font not embedded”报错,提供了三种核心修复方案。首先,详细解析了使用Adobe Acrobat DC进行专业修复的完整操作流程与关键技巧。其次,介绍了无需付费软件的开源与免费工具链(如Ghostscript)解决方案。最后,从源头出发,指导LaTeX和Microsoft Word用户在生成PDF时正确设置以确保字体自动嵌入,帮助研究者彻底解决格式问题,确保投稿顺利。

ujm56789012的博客 521

ATL中编译期间的虚函数调用机制

 ATL中编译期间的虚函数调用机制     其实也不是真正的虚函数,只是通过模板的方式实现了虚函数的功能。template class B1{public:     void SayHi()     {        T* pT = static_cast(this);   // HUH?? 我将在下面解释         pT->PrintClassName();    }protected: 

做好我自己 579

如何评价智能座舱的好坏之指标拆分?

智慧服务实际上是有一些比较实用的功能的,常用的有后视镜自动伸展、离车自动落锁、座椅记忆等,但这部分前装率较高,各家的区分度不高,所以一级指标中占比最低(10%)。不过呢,规则里的权重仅有5%,或许是考虑到比较成熟,没有区分度。全景环视明显拔得头筹,占比85%,这对于我这种没有倒车影像不敢倒车的人,举双手赞成,但还是要看其效果,比如,现在,测评规则的颁布实际上也让我们看到,智能座舱的技术方案正走向成熟,并将走进规范化运作。,占比 60%,这与我们对座舱最直接的认知符合,

shuiqingyan的博客 1086

从像素到光影:解析《黑神话:悟空》中Nanite与Lumen的协同渲染革命

本文深度解析了《黑神话:悟空》如何利用Unreal Engine 5的Nanite与Lumen技术实现渲染革命。Nanite处理亿级多边形几何细节,无需传统LOD优化;Lumen提供全动态全局光照,实现实时光线反弹。两者协同工作,为游戏带来电影级视觉体验,重塑了实时渲染的技术标准与工作流程。

f6g7h8i9j的博客 425

ATL的编译期间的虚函数调用机制

      在ATL或者WTL中,定义类的时候经常看到如下写法: class CAboutDlg : public CStdDialogImpl { ... },今天看了一篇文章才知道这么写的妙处,以前用的不多,也没有深究,惭愧,惭愧,C++真的博大精深,奥妙无穷。       一般的C++程序对虚函数的支持都是通过虚函数表在运行时处理,ATL中这样的写法通过模板在编译时不用虚函数表就处理

思雨轩的专栏 665

halcon与QT联合:(5.1)瓶盖检测以及QT界面搭建

halcon实现瓶盖检测与定位代码: dev_close_window() dev_open_window(0, 0, 1024, 1024, 'black', WindowHandle) list_image_files('C:/Users/TIMI/Desktop/picture/circle', 'default', [], ImageFiles) for J := 1 to 4 by 1 *read image read_image(halconImage, ImageFil

m0_37957160的博客 1048

ATL-Style模板 不用虚函数实现 多态

最近在看ATL和WTL,比较有意思的是,它用类模板来实现多态,子类通过类模板将真实的类型传给父类,这样父类就知道真实的类型了。简单的例子:#include //Coding by nyzhltemplateclass CBase{public: void Display() { T* pT = static_cast(this); pT->Print()

&*焰火*&天堂 2168

YOLOv8独家原创改进:原创自研 | 创新自研CPMS注意力,多尺度通道注意力具+多尺度深度可分离卷积空间注意力,全面升级CBAM

自研CPMS,多尺度通道注意力具+多尺度深度可分离卷积空间注意力,全面升级CBAM

①答疑群聊服务;②YOLO大模型知识问答系统;③计算机视觉论文生成智能体; 3808

《AI Coding手册:Claude Code、OpenAI Codex、OpenClaw深度解析与实战指南》

AI编码技术经历了从简单规则匹配到深度神经网络,再到当前智能体(Agent)架构的范式转变。第一阶段:静态代码补全(2010-2018)早期工具如IntelliSense、Emmet基于语法分析和固定模板提供代码提示。这类系统依赖预定义规则,缺乏语义理解能力,仅能处理局部代码片段。第二阶段:统计语言模型(2018-2020)以GPT-2、TabNine为代表,引入基于大规模代码训练的神经语言模型。

AI天才研究院 1044

ATL布幔之下的秘密(1)

原作者:Zeeshan Amjad原文链接:http://www.codeproject.com/atl/atl_underthehood_.asp 介绍  在本系列的教程中,我要讨论一些ATL的内部工作方式以及它所使用的技术。   在讨论的开始,让我们先看看一个程序的内存分布。首先,编写一个简单的程序,它没有任何的数据成员,你可以看看它的内存结构。程序1.#include using names

马说@CSDN 3250

【PointNet++】使用Open3D将ShapeNet数据集txt文件保存为ply

ShapeNet数据集的txt格式为xyzrgb+label,为了可视化点云,将txt文件保存为pcd或ply。

纵马踏花向自由 753

ATL布幔下的秘密之窗口类的秘密

介绍   很多人认为ATL只是用来编写COM组件的,其实你也可以使用ATL中的窗口类来创建基于窗口的应用程序。虽然你可以将基于MFC的程序转换为ATL,但是ATL中对于UI(译注:用户界面)组件的支持太少了。所以,这就要求你需要自己编写很多代码。例如,在ATL中没有文档/视图,所以在你想使用它的时候就需要自己实现了。在本篇中,我们将要探究一些关于窗口类的秘密,以及ATL技术实现的秘密。WTL(Wi

★果冻★的专栏 1515

ET2012新版免狗免安装绿色软件

ET2012新版免狗免安祖昂绿色软件。,新界面,新视觉,

visio安装包

Microsoft Office Visio 是一款专业的制图软件。利用经过更新的形状、协作工具和数据链接图表,创建专业级图表以简化复杂的信息。

ATL布幔下的秘密之模板技术

 介绍   如果你是个模板的高手,你就可以将ATL的学习作为一种享受。在这一节中,我将要尝试解释一些ATL使用的模板技术。我不能保证你读完本节后能成为一个模板高手,只能是尽我所能让你在读完本文后能够更轻松地理解ATL的源码。   程序35. #include <iostream>using namespace std;template

★果冻★的专栏 1377

vb 6.0并口开发操作实例

vb6并口开发,简体中文,试例内有inpout32.dll动态链接库

ATL布幔之下的秘密(5)

原作者:Zeeshan Amjad原文链接:http://www.codeproject.com/atl/atl_underthehood_5.asp点这里下载本文配套源代码介绍  很多人认为ATL只是用来编写COM组件的,其实你也可以使用ATL中的窗口类来创建基于窗口的应用程序。虽然你可以将基于MFC的程序转换为ATL,但是ATL中对于UI(译注:用户界面)组件的支持太少了。所以,这就要求你需要

马说@CSDN 2806

pscad IEEE标准系统搭建说明

pscad IEEE标准系统搭建说明,包含典型的IEEE 9节点、14节点、30节点、39节点和118节点系统。内含标准测试系统的原始数据和潮流结果。

上一篇: 2004.07.12,Mon - 万事俱备
下一篇: 2004.07.14,Wed - 长吁一口气
李马
博客等级 码龄23年 494粉丝 291原创
评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值