Is partial class template specialization the answer to this design problem?

前端 未结 4 1282
被撕碎了的回忆
被撕碎了的回忆 2020-12-30 15:12

Say you have a class who\'s job it is to connect to a remote server. I want to abstract this class to provide two versions, one that connects through UDP and the other throu

4条回答
  •  青春惊慌失措
    2020-12-30 15:34

    I would use the curious recuring template pattern, aka Five Point Palm Exploding Alexandrescu Technique:

    template 
    class Transmit
    {
    public:
       void send(...)
       {
          _U.send(...)
       };
    
    private:
        Underlying _U;
    };
    
    class Tcp
    {
    public:
       void send(...) {};
    };
    
    class Udp
    {
    public:
       void send(...) {};
    };
    

    There would probably be many more template parameters and sub classes but you get the idea, you can also use static methods.

    By the way template code is generally more efficient but also much bigger.

提交回复
热议问题