Singleton pattern and std :: unique_ptr

std::unique_ptruniquely controls the object to which it points, and therefore does not use reference counting. Singleton provides the creation of only one object using reference counting.

Will it then std::unique_ptrperform identically singleton?

+5
source share
4 answers

Singleton provides only one instance per type.

A unique_ptrprovides only one smart pointer for any instance.

+11
source

Will std :: unique_ptr execute identically with a singleton?

. , Foo, . , Foo.

std::unique_ptr<Foo> , Foo, Foo ( unique_ptr, ). , Foo, .

+5

std::unique_ptr , , .

singleton , unique_ptrs, . , , .

, ,

"_ptr .

. :

T* nt = new T;
std::unique_ptr<T> up1(nt);
std::unique_ptr<T> up2(nt);

, , , . , unique_ptr, , unique_ptr , , api, .

, () , / unique_ptr. .

+3

Correct me if I am wrong, but as far as I remember, singelton is a class that can have only one instance. This is completely different. Then no.

0
source

All Articles