Overloading the assignment operator
-
I can't find any documentation on this anywhere ... is it possible? I have a class: public Player { ... private fields ... public methods } And there are many times I need to simply say this Player equals that Player and have all the values from one copied into the next. I have created a pubic method inside player, called "Copy()" which simply creates a new player from the current players data and returns it. So I can do something like: player1 = player2.Copy() But this is VERY annoying. Can I simply overload the assignment operator so that I can do: player1 = player2 and it will copy all the data from player 2 into player 1, rather than setting player1 equal to the reference of player2?
-
I can't find any documentation on this anywhere ... is it possible? I have a class: public Player { ... private fields ... public methods } And there are many times I need to simply say this Player equals that Player and have all the values from one copied into the next. I have created a pubic method inside player, called "Copy()" which simply creates a new player from the current players data and returns it. So I can do something like: player1 = player2.Copy() But this is VERY annoying. Can I simply overload the assignment operator so that I can do: player1 = player2 and it will copy all the data from player 2 into player 1, rather than setting player1 equal to the reference of player2?
You can use a struct instead of a class if you dont need inheritance. xacc.ide-0.1-rc2 released! Download and screenshots