C++ Template - Multiple types

前端 未结 3 1030
旧时难觅i
旧时难觅i 2021-01-30 17:08

consider the following class template:

template 
class MyClass
{
   void MyFunc();
};

template 
void MyClass::MyFunc()
{
          


        
3条回答
  •  清酒与你
    2021-01-30 17:22

    Try this one :

    template 
    class MyClass
    {
    public:
        static void MyFunc2(T2* data);
    };
    
    template 
    void MyClass::MyFunc2(T2* pData)
    {
        cout << "dummy " << *pData<< "\n";
    }
    

    Then

    int main()
    {
        cout << "Hello World!\n"; 
        MyClass a;
        int *b = (int*)malloc(sizeof(int));
        *b = 5;
        a.MyFunc2(b);
    }
    

    Output

    Hello World!
    dummy 5
    

提交回复
热议问题