将类型名称作为参数传递给成员模板函数

时间:2015-02-26 09:26:27

标签: c++ templates

我有两个类似的结构,包装略有不同。它们用在另一个类的成员函数中。因此,我可以编写一些模板方法,将这些结构作为模板参数,以避免重复。那么如何传递struct name本身进行模板初始化呢?

  struct header
    {
        char symbol[6];
    };

    struct headerV8
    {
       int id;
       char symbol[8];
    };

    class connection
     {
        connectionMgr _mgr;

        public:

        // here I want to pass T as only type; Class itself is not a template class 
        // but its method is template function.

        template<class T>
            void request(T); 
     };


     //template parameter is used to decide which struct to use header or headerV8.
     <template T>
     void connection::request(T)  
      {
         T a;
         strcpy(a.symbol, "AAPL");
         cout << a.symbol;
         mgr.socketSend(a);
      }

  calls to request
     if(versionV8)
         conn.request(headerV8);
     else
         conn.request(heaver);

0 个答案:

没有答案
相关问题