Template detects if T is pointer or class

后端 未结 4 512
[愿得一人]
[愿得一人] 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:37

    I'm aware that my answer is not exactly about what you are asking, but maybe it could help.

    I believe your intention is to have List class with one insert method (not two of them) and behaviour of this method should depend on your template parameter. For this you could write a specialization of your class for pointers. Then basic template would be used for non pointer types and specialization would be used for pointer types.

    Your code would look like this:

    template 
    class List
    {
    public:
    
        void insert(const Object & x)
        {
            // call when Object is MyClass
        }
    };
    
    template 
    class List
    {
    public:
    
        void insert(Object * x)
        {
            // call when Object is MyClass*
        }
    };
    
        

    提交回复
    热议问题