More effective C + + smart pointers

Source: Internet
Author: User

Smart pointers have a very powerful ability, and careful and judicious choice can bring great benefits. I do not deny the ability of smart pointers, although I have previously denied auto_ptr. Perhaps because of my own ability limitations, not realize the benefits of auto_ptr, but this possibility I feel is not big. But Auto_ptr is the simplest smart pointer, there are a lot of works around it, including Boost, Loki, Ace and so on, but unfortunately there is no one I can say I am familiar with, then this article is only as a primer, on this basis, should read boost, Loki, Ace related source code.

The core of Smart pointer is to realize

template <class T>
T& SmartPointer<T>::operator*() const;
template <class T>
T& SmartPointer<T>::operator->() const;

The construction and deconstruction of smart pointer is an art, which derives many different types of smart pointer. Never expect smart pointer to behave like a native pointer, although it can be implemented by implicit conversions, but often the consequences are catastrophic.

Meyers gives an elegant approach to implicit conversions:

template<class T>
class TestTemplate
{
public:
TestTemplate(T* ptr = 0):pointee(ptr){}
template<class newType>
operator TestTemplate<newType>()
{
return TestTemplate<newType>(pointee);
}
private:
T* pointee;
};

Unfortunately, such programs cannot be compiled in VC6, and it seems that VC6 does not support declaring Novirtual member function as templates, but VC7 can. Here the place needs attention four point technology:

(1) Variable matching rules for function calls

(2) Implicit type conversion function

(3) Template functions of the body

(4) member function templates. I admit, it's a bit too deep.

The conversion between const and Non-const in the smart pointer is also a great learning, I saw Meyers use unions to do the implementation. This is not my favorite practice, I think the risk is still relatively large.

is Smart pointer worth using? This is not a question I can answer, but in the past, I seem to have rarely used it. Maybe it was my loneliness that caused this situation, but at a deeper level, I needed to read more about the smart pointer implementations. And more important is the need to learn to smart pointer debugging, which seems not easy.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.