lundi 9 août 2021

How to avoid copy assignment operator c++ for a shared pointer

Consider the line below to create an object foo of class creature:

foo = creature(choice);

This creates a temporary creature object before assigning it to foo. Which also means that the destructor will be called. If I don't want this to happen, and I don't want to implement a copy assignment operator, I will then create it with this:

creature foo(choice);

How do I do it likewise for the line below? Again, creature's destructor will get called, and I want to avoid this.

std::shared_ptr<creature> creature_sp = std::make_shared<creature>(creature(choice));

Aucun commentaire:

Enregistrer un commentaire