zoukankan      html  css  js  c++  java
  • 友元模板类使用

    前言:

         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;

    }


  • 相关阅读:
    2014-04-23 总结
    14-5-13
    PHP
    14-5-8
    ajax
    14-5-6
    14-5-5
    PHP初解
    14-4-30
    14-4-29
  • 原文地址:https://www.cnblogs.com/mokliu/p/2138787.html
Copyright © 2011-2022 走看看