这是我对一些C++ 11研究示例的实现.我让所有构造函数和析构函数打印到控制台.但令人惊讶的是,我得到了两次构造函数但是析构函数三次.
看起来意想不到的是0x7fff5fbff6d0.创建此对象时?但为什么没有关联的构造函数调用?
为什么会这样?
template<typename T>
class ArrayWrapper{
public:
ArrayWrapper():data_(nullptr), size_(0){
cout << "Default ctor called "<< this <<endl;
}
ArrayWrapper(size_t n, const T& val) : data_(new T[n]), size_(n){
cout << "ctor_n_val called "<< this << endl;
for_each(data_, data_+size_, [&](T& elem){ elem=val; });
}
ArrayWrapper(const ArrayWrapper& other): data_(new T[other.size_]), size_(other.size_)
{
cout << "copy ctor called "<< this <<endl;
copy(other.data_, other.data_+other.size_, data_);
}
ArrayWrapper(ArrayWrapper&& other): data_(other.data_), size_(other.size_)
{
cout << "move ctor called"<<endl;
other.data_ = nullptr;
other.size_ = 0;
} …Run Code Online (Sandbox Code Playgroud)