Auto

xiaoxiao2021-03-13  190

The way to apply for pointers in C is usually NEW and DELETE. However, there is a powerful template class in standard C to automatically help you release memory automatically when you are not used. Let's take a look at the usage.

Use

Std :: auto_ptr m_example (new myclass ());

Method

Std :: auto_ptr m_example;

m_example.reset (new myclass ());

Using the law three (assignment operation of the pointer)

Std :: auto_ptr m_example1 (new myclass ());

Std :: auto_ptr m_example2 (new myclass ());

m_example2 = m_example1;

Then C will reclaim the memory to the m_example, so that the value of the m_example is NULL, so in C , it should be absolutely avoided in the container. Avoid the following code

Vector > m_example;

When using the algorithm to operate, you can't avoid the implementation of the elements in the container in the STL, so that multiple elements in the container are set null, and this is not what we want to see.

转载请注明原文地址:https://www.9cbs.com/read-129160.html

New Post(0)