条款43:学习处理模板化基类内的名称

代码:

#pragma once
#include <iostream>
#include <string>
using namespace std;
class CompanyA
{
public:

    CompanyA()
    {
    }

    virtual ~CompanyA()
    {
    }

    void sendCleartext(const std::string& msg){
        cout << "CompanyA.sendCleartext:" << msg << endl;
    }
    void sendEncrypted(const std::string& msg){
        cout << "CompanyA.sendEncrypted:" << msg << endl;
    }
};

//-------------------------------------
#pragma once
#include <iostream>
#include <string>
using namespace std;
class CompanyB
{
public:

    CompanyB()
    {
    }

    virtual ~CompanyB()
    {
    }

    void sendCleartext(const std::string& msg){
        cout << "CompanyB.sendCleartext:" << msg << endl;
    }
    void sendEncrypted(const std::string& msg){
        cout << "CompanyB.sendEncrypted:" << msg << endl;
    }
};



//-------------------------------------

#pragma once
#include <iostream>
#include <string>
using namespace std;
class CompanyC
{
public:

    CompanyC()
    {
    }

    virtual ~CompanyC()
    {
    }

    //void sendCleartext(const std::string& msg){
    //    cout << "CompanyC.sendCleartext:" << msg << endl;
    //}
    void sendEncrypted(const std::string& msg){
        cout << "CompanyC.sendEncrypted:" << msg << endl;
    }
};


//-------------------------------------
#pragma once
#include <string>
using std::string;
class MsgInfo
{
    string msg;
public:

    MsgInfo(string msg)
        : msg(msg)
    {
    }

    ~MsgInfo()
    {
    }

    string getMsg(bool bEncrypt = false) const
    {
        if (bEncrypt){
            return "[encrypt]" + msg;
        }
        return msg;
    }
};



//-------------------------------------

#pragma once

#include "MsgInfo.h"

template<class Company>
class MsgSender
{
public:

    MsgSender()
    {
    }

    virtual ~MsgSender()
    {
    }

    void sendClear(const MsgInfo& msgInfo)
    {
        std::string msg;
        msg = msgInfo.getMsg();
        Company c;
        c.sendCleartext(msg);
    }
    void sendSecret(const MsgInfo& msgInfo)
    {
        std::string msg;
        msg = msgInfo.getMsg(true);
        Company c;
        c.sendEncrypted(msg);
    }
};

//特化
template<>
class MsgSender<CompanyC>
{
    public:

        MsgSender()
        {
        }

        virtual ~MsgSender()
        {
        }


        void sendSecret(const MsgInfo& msgInfo)
        {
            std::string msg;
            msg = msgInfo.getMsg(true);
            CompanyC c;
            c.sendEncrypted(msg);
        }
};
//-------------------------------------

#pragma once
#include <iostream>

#include "MsgSender.h"

using std::cout;

template<class Company>
class LoggingMsgSender : protected MsgSender<Company>
{
public:

    LoggingMsgSender()
    {
    }

    virtual ~LoggingMsgSender()
    {
    }

    void sendClearMsg(const MsgInfo& msgInfo)
    {
        cout << "sendClear之前 写 log 。。。\n";
        //sendClear(msgInfo);//注:使用的VS2013,可以编译通过。(书上说会编译报错。可能之后的优化了)
        this->sendClear(msgInfo);
        cout << "sendClear之后 写 log 。。。\n";
    }
    void sendSecretMsg(const MsgInfo& msgInfo)
    {
        cout << "sendSecret之前 写 log 。。。\n";
        sendSecret(msgInfo);//注:使用的VS2013,可以编译通过。(书上说会编译报错。可能之后的优化了)。
        cout << "sendSecret之后 写 log 。。。\n";
    }
};


//-------------------------------------

#include <iostream>
#include "CompanyA.h"
#include "CompanyB.h"
#include "CompanyC.h"
#include "MsgSender.h"
#include "LoggingMsgSender.h"
using namespace std;



int main(){
    //debug 探测内存泄露
    _CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF);
    //int* p = new int;

    MsgInfo msgInfo("hello world.");
    
//    MsgSender<CompanyA> ma;
//    ma.sendClear(msgInfo);
//    ma.sendSecret(msgInfo);
//
//    MsgSender<CompanyB> mb;
//    mb.sendClear(msgInfo);
//    mb.sendSecret(msgInfo);
//
//    MsgSender<CompanyC> mc;
////    mc.sendClear(msgInfo);
//    mc.sendSecret(msgInfo);

    LoggingMsgSender<CompanyA> lma;
    lma.sendClearMsg(msgInfo);
    lma.sendSecretMsg(msgInfo);
    

    return 0;
}
//-------------------------------------
View Code

 

 

++

posted @ 2022-12-22 17:04  htj10  阅读(13)  评论(0编辑  收藏  举报
TOP