简体   繁体   English

C ++模板类,如何针对特定情况声明副本构造函数?

[英]C++ template class, how to declare a copy constructor for a specific situation?

Hi (English is not my first language, please understand me even if I make mistakes! thank you!!) 嗨(英语不是我的母语,即使我犯了错误也请理解我!谢谢!!)

I'm writing a template class that can contain a pointer. 我正在写一个可以包含指针的模板类。

template <typename T>
class SmartPtr {
private:
      T value;
public:
      SmartPtr() {};
      ~SmartPtr() {};

     SmartPtr(T* a)
     {
        this->value = *a;
     }
     SmartPtr(SmartPtr* a)
     {
          this->value = a->get_Value();
     }
     SmartPtr(SmartPtr const* a)
     {
          this->value = a->get_Value();
     }

     T get_Value()const{
          return this->value;
     }
};

This is template class called SmartPtr, and 这是称为SmartPtr的模板类,并且

class Test
{
public:
      Test() { std::cout << "Test::Test()" << std::endl; }

      Test(Test const&) { std::cout << "Test::Test(Test const&)" << std::endl; }

      ~Test() { std::cout << "Test::~Test()" << std::endl; }

      Test& operator=(Test const&)
      {
           std::cout << "Test& Test::operator=(Test const&)" << std::endl;
           return *this;
      }

      void print() const { std::cout << "Test::print() const" << std::endl; }
      void print() { std::cout << "Test::print()" << std::endl; }
};

this is my Test class. 这是我的测试课。

When I declare 当我宣布

SmartPtr<Test> ptr_t1 = SmartPtr<Test>(new Test); in my main.cpp, 在我的main.cpp中,

the result after compiling is 编译后的结果是

Test::Test()
Test::Test()
Test& Test::operator=(Test const&)
Test::~Test()

but the result that I want to get is 但是我想要得到的结果是

Test::Test()
Test::~Test()

Is there a specific template class copy contructor that I need to write in this situation? 在这种情况下是否需要编写特定的模板类副本构造函数?

Thank you so much for your patience! 非常感谢您的耐心配合!

The reason is because of inside SmartPtr there is the value member variable: 原因是由于在SmartPtr内部存在value成员变量:

template <typename T>
class SmartPtr {
private:
      T value; // here T is of class Test
... other stuff ...
}

When declaring 申报时

SmartPtr<Test> ptr_t1 = SmartPtr<Test>(new Test);

ptr_t1 is construct, thus it's value is constructed. ptr_t1是构造函数,因此它的值是构造函数。 So that's the first Test() constructor call. 这是第一个Test()构造函数调用。 The second constructor is the new Test (obviously). 第二个构造函数是new Test (显然)。 Then, the SmartPtr is constructed, and inside, the this->value = *a; 然后,构建SmartPtr ,并在内部构建this->value = *a; calls the Test() assignment operator. 调用Test()赋值运算符。

Finally the SmartPtr<Test>(new Test) object is destructed, calling the destructor on the internal value object. 最后, SmartPtr<Test>(new Test)对象被破坏,在内部value对象上调用析构函数。

Note also because there was a new Test called, but no delete , there is a memory leak as well. 另请注意,因为有一个名为的new Test ,但没有delete ,也有内存泄漏。

In order for only the constructor and destructor to be called, simply call the constructor directly: 为了只调用构造函数和析构函数,只需直接调用构造函数即可:

SmartPtr ptr_t1(new Test); SmartPtr ptr_t1(新测试);

Also, your SmartPtr class should store a pointer instead of the value. 另外,您的SmartPtr类应该存储一个指针而不是值。 The value can reside in memory allocated by the call to new. 该值可以驻留在调用new分配的内存中。 Instead of: 代替:

private: T value; 私人:T值;

write: 写:

private: T* value; 私人:T *值;

This will ensure that the value isn't copied by the constructor, but is instead just pointed to. 这将确保该值不被构造方法复制,而是仅被指向。 The value will still reside at the memory allocated by new. 该值仍将驻留在new分配的内存中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM