原作者:zeeshan amjad
原文链接:http://www.codeproject.com/atl/atl_underthehood_.asp 【相关文章:在HTM中引用VB6的OCX控件】 【扩展阅读:qmail队列清除办法之一】 【扩展信息:一个男人和三个女人的故事[《.net框架】介绍
在本系列的教程中,我要讨论一些atl的内部工作方式以及它所使用的技术。在讨论的开始,让我们先看看一个程序的内存分布。首先,编写一个简单的程序,它没有任何的数据成员,你可以看看它的内存结构。
程序1. #include <iostream> using namespace std;class class {
};int main() {
class objclass; cout << "size of object is = " << sizeof(objclass) << endl; cout << "address of object is = " << &objclass << endl; return 0; } 这个程序的输出为: size of object is = 1 address of object is = 0012ff7c 现在,如果我们向类中添加一些数据成员,那么这个类的大小就会是各个成员的大小之与。对于模板,也依然是这样: 程序2. #include <iostream> using namespace std;template <typename t>
class cpoint { public: t m_x; t m_y; };int main() {
cpoint<int> objpoint; cout << "size of object is = " << sizeof(objpoint) << endl; cout << "address of object is = " << &objpoint << endl; return 0; } 现在程序的输出为: size of object is = 8 address of object is = 0012ff78 那么,再向程序中添加继承。现在我们使point3d类继承自point类,然后来看看程序的内存结构: 程序3. #include <iostream> using namespace std;template <typename t>
class cpoint { public: t m_x; t m_y; };template <typename t>
class cpoint3d : public cpoint<t> { public: t m_z; };int main() {
cpoint<int> objpoint; cout << "size of object point is = " << sizeof(objpoint) << endl; cout << "address of object point is = " << &objpoint << endl;cpoint3d<int> objpoint3d;
cout << "size of object point3d is = " << sizeof(objpoint3d) << endl; cout << "address of object point3d is = " << &objpoint3d << endl;return 0;
} 程序的输出为: size of object point is = 8 address of object point is = 0012ff78 size of object point3d is = 12 address of object point3d is = 0012ff6c 这一程序演示了派生类的内存结构,它表明派生类的对象所占据的内存为它本身的数据成员与它基类的成员之与。 当虚函数加入到这个派对中的时候,一切就变得都有意思了。请看下面的程序: 程序4. #include <iostream> using namespace std;... 下一页