我的复制构造函数没有被调用,我不确定为什么。这是我的代码:
template <typename T>
class SmartPtr
{
public:
explicit SmartPtr(T *p) : m_p(p) { cout << "ctor" << endl; }
SmartPtr(const SmartPtr& p) : m_p(p.m_p) { cout << "copy ctor" << endl;}
private:
T* m_p;
};
int main()
{
SmartPtr<int> pt4 = SmartPtr<int>(new int);
}
输出只是“ctor”。看起来使用了默认的复制构造函数。如果我添加“显式”,则它不会编译,并给出错误:
"error: no matching function for call to ‘SmartPtr<int>::SmartPtr(SmartPtr<int>)’"
我在这里做错了什么?