• There is NO official Otland's Discord server and NO official Otland's server list. The Otland's Staff does not manage any Discord server or server list. Moderators or administrator of any Discord server or server lists have NO connection to the Otland's Staff. Do not get scammed!
  • 2026 staff recruitment is open! Check it out and consider applying!

Solved C++ overloading operator on an assignment in a constructor using delete TFS 1.2

CaseyJones

Banned User
Joined
Jul 29, 2016
Messages
4
Reaction score
1
Can anyone explain to me the purpose of this fragment of code I am really having trouble wrapping my head around it.

Code:
        // non-copyable
        Player(const Player&) = delete;
        Player& operator=(const Player&) = delete;

I have a general idea of what is going on here Player is a constructor its argument is a reference to a Player object the overloaded constructor of Player is an assignment with also an argument of a reference to Player which is assigned delete to prevent a default constructor being called.

If I am wrong about its ok as I said I am having a tough time wrapping my head around this. Any help on this matter will be greatly appreciated.

Correction the overloaded constructor of Player has an overloaded assignment operator which is passed a reference of type Player, yes both arguments in each constructor are const's.

One more thing I would like to note there is no definition of the overloaded operator for the constructor defined in player.cpp maybe this is what is throwing me off?

I guess it doesn't need to be defined? As delete is destroying the constructor... this is really frustrating trying to understand.

bump
 
Last edited by a moderator:
It means that you remove copy contructor and assign operator i think. I believe it was done to prevent player object from beeing copied.
 
Last edited:
It means that you remove copy contructor and assign operator i think. I believe it was done to prevent player object from beeing copied.
Thanks for the reply but that doesn't really tell me much about the code or the reason why there is no definition for the overloaded operator on this constructor or the use of delete.

Never mind I found a suitable explanation on another site.
 
Last edited by a moderator:
This is a copy constructor:
Code:
Player(const Player&);
And this is a copy assignment operator:
Code:
Player& operator=(const Player&);
By declaring these functions as deleted (by appending '=deleted;') you are telling the compiler that these functions cannot be used. The purpose is to keep a Player object from being copied and potentially causing memory issues.
 
Back
Top