王朝网络
分享
 
 
 

WTL for MFC Programmers(3)

王朝vc·作者佚名  2006-01-09
宽屏版  字体: |||超大  

ATL-style templates

Even if you can read C++ templates without getting a headache, there is two things ATL does that might trip you up at first. Take this class for example:

关于C++的模版定义,ATL有两个让人头疼的事情。注意以下的例子:

class CMyWnd : public CWindowImpl<CMyWnd>

{

...

};

That actually is legal, because the C++ spec says that immediately after the class CMyWnd part, the name CMyWnd is defined and can be used in the inheritance list. The reason for having the class name as a template parameter is so ATL can do the second tricky thing, compile-time virtual function calls.

上面的代码是合法,因为C++规定只要在class CMyWnd定义部分的后面,就可以使用CMyWnd,当然也可以将它用在基类表的定义中。这就是为什么类名可以像一个模板参数那样使用,也正是由于这样的特性,ATL就有了第二件狡猾的设计,编译时虚函数调用。

(如果在class CMyWnd的后面马上就可以使用CMyWnd,那么能不能让一个类作自己的子类呢?好像可以,其实是行不通的,因为编译器会去找基类的定义,如果自己做自己的基类,编译器会提示基类没有定义 – 蜗牛手记)

To see this in action, look at this set of classes:

注意看下面的类定义:

template <class T>

class B1

{

public:

void SayHi()

{

T* pT = static_cast<T*>(this); // HUH?? I'll explain this below

pT->PrintClassName();

}

protected:

void PrintClassName() { cout << "This is B1"; }

};

class D1 : public B1<D1>

{

// No overridden functions at all

};

class D2 : public B1<D2>

{

protected:

void PrintClassName() { cout << "This is D2"; }

};

main()

{

D1 d1;

D2 d2;

d1.SayHi(); // prints "This is B1"

d2.SayHi(); // prints "This is D2"

}

The static_cast<T*>(this) is the trick here. It casts this, which is of type B1*, to either D1* or D2* depending on which specialization is being invoked. Because template code is generated at compile-time, this cast is guaranteed to be safe, as long as the inheritance list is written correctly. (If you wrote

class D3 : public B1<D2>

you'd be in trouble.) It's safe because the this object can only be of type D1* or D2* (as appropriate), and nothing else. Notice that this is almost exactly like normal C++ polymorphism, except that the SayHi() method isn't virtual.

语句static_cast<T*>(this)是一个骗局。它通过这个特殊的调用将类型是B1*的this转换为D1*或者D2*。因为模板的代码是在编译时生成的,只要基类烈表示正确的,这种转换保证是安全的。(如果你写出这样的语句:class D3 : public B1<D2>,那你将于遇到麻烦)。这种安全是因为this对象只能根据定义转换为D1*或D2*,而不能转换为其他的什么东西。这就像标准C++的多态性一样的,只是SayHi()函数不是虚函数而已。

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
>>返回首页<<
推荐阅读
 
 
频道精选
 
静静地坐在废墟上,四周的荒凉一望无际,忽然觉得,凄凉也很美
© 2005- 王朝网络 版权所有