Template detects if T is pointer or class

后端 未结 4 510
[愿得一人]
[愿得一人] 2020-12-19 04:27

Considering the following code:

class MyClass
{
    ...
};

template 
class List
{
public:

    void insert(const Object & x)
             


        
4条回答
  •  臣服心动
    2020-12-19 04:52

    This does the trick:

    template 
    class List
    {
    public:
    
        template
        void insert(const C & x)
        {
            // call when Object is MyClass
            std::cout << "1" << "\n" ;
        }
    
        template
           void insert(P* p)
        {
            // call when Object is MyClass*
            std::cout << "2" << "\n" ;
        }
    } ;
    

    Here is a working example.

提交回复
热议问题