I’m trying to make a step based puzzle game (kind of like Baba Is You) and I need to store the state of the level at each step. So I’m using classes for the level, entities and some other things, and I was wondering if I can use default constructors like "Level(Level &&)" or "Level(const Level &)" I saw proposed by autocompletion to make a copy of the level as it is, but I can’t grasp how they work from the documentation. The main idea was to have each level have its previous step (the copy of the level at the previous step) as an attribute, so that the game can work out any step recursively.
The question is : is any of this possible?
Because the only other way I see to do this is by making a method for my class that will simply create a new blank level and set all the attributes of this new level to those of the actual level, then return it and store it in the main scope. And this seems to be pretty bad because it requires to have evrything public or another method that sets every private attribute to a new value.
If you want to give a look at the code it’s here : https://github.com/Leroymilo/SwapCpp/tree/main/Experimental but it’s not really clean since I’m starting in C++, and I believe from what I know that this is more of a technical question that doesn’t involve correcting what I did (hopefully).
>Solution :
use default constructors like
Level(Level &&)orLevel(const Level &)
-
Level(Level &&)is the move constructor. You don’t want that, because you don’t want to damage the original object -
Level(Level const &)is the copy constructor, which is for making a duplicate of an existing object without altering the original.This is exactly what you’re asking for, and should be covered pretty early by any competent book in the section on writing classes.
-
for reference, "default constructor" means specifically
Level()– the constructor with no arguments. This is a well-known term that should also be described in any competent book.
The compiler-generated versions of these constructors are sometimes described as defaulted (and can be requested like
Level(Level const &) = default;
in situations where they wouldn’t be generated automatically, or just to make it explicit) – but it’s important not to confuse them with the default constructor (which may itself be defaulted if you don’t provide one).
Whether the compiler-generated copy constructor will actually do the right thing for you depends entirely on the data members and semantics of your class, which you haven’t shown.
In general, it will work as a shallow copy so long as you don’t use owning raw pointers or non-copyable types like std::unique_ptr. The std::shared_ptr is particularly suitable for automatic shallow copying where you want shared ownership.
If you want a deep copy, you either need to write the copy constructor by hand or use (ie, find or write) a deep-copying smart pointer.
Either way, see the Guidelines on Resource Management for recommendations that will help the compiler generate correct constructors and destructors for you.