The lifetime of an object in these two cases is different. C ++ ensures that static local objects will be destroyed in the reverse order before they are built. In both cases, construction will occur when it GetInstanceis first called.
b , new. b , , . "", ( ) b .
:
class B {
public:
static const B* GetInstance() {
static std::unique_ptr<B> b( new B );
return b.get();
}
};
B::~B() ( ) , b , , .
. . , , new, .