jeudi 31 janvier 2019

Creating a smart pointer member of class referencing itself is a elegant design pattern in c++?

I would like to know if following code is a good pattern in C++?

There is no problem at all. The code works. But I would like to know if this can lead to some sort of problem.

#include <iostream>
#include <memory>

template <typename T>
class Class {
    public:
        std::shared_ptr<Class> shared_ptr;

        Class() : shared_ptr(this) {}
        ~Class() { shared_ptr.reset(); }
};

Aucun commentaire:

Enregistrer un commentaire