友元模板类使用

前言:

     ATL/WTL中 程序设计中用模板类来附加新属性和实现接口。为了不破坏宿主类对外的封装性,需要定义友元类来解决这个问题。


#include "stdafx.h"

template<class T>

class TemplateDemo1

{

public: 

TemplateDemo1()

{

}

public:

void Do()

{

T* pThis = static_cast<T*>(this);

pThis->iUncoun = 120;

}

};

class __declspec(novtable) Demo1 : public TemplateDemo1<Demo1>

{

public: 

friend class TemplateDemo1<Demo1>;

Demo1()

{

}

public:

int GetIValue()

{

return iUncoun;

}

private:

int iUncoun;

};

int _tmain(int argc, _TCHAR* argv[])

{

Demo1 demo;

demo.Do();

printf("%d", demo.GetIValue());

return 0;

}


posted @ 2011-07-25 11:20  MokLiu  阅读(287)  评论(0编辑  收藏  举报