initializing object back to their default value

I have a class Square , Player . Player consists of string name and another object XYZ.
Suppose i have to destroy player in the Square class.
Destroy means players name becomes NULL to show that there is no player on the square.
How should it be done?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
class Square
{
private :
	Player player;

public :
	Square(Player p)
	{
		player = p;
	}

	void destroy()
	{
		// what should come here?
		// is it right? Why or why not
		player = new Player("" , new XYZ());
	}
};
closed account (SECMoG1T)
Here is another way
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
class Player
{
   private:
        std::string name;
        bool destroyed;
   public:
        Player(std::string nm): name(nm),destroyed(false){}
        bool is_destroyed() const {return destroyed;}
        void destroy() {destroyed=true;}
        void revive() {destroyed=false;}
};

class Spuare
{
   private:
        Player player;
   public:
        Square(Player p): player(p){}
        void destroy_player()
           {
                 player.destroy();
            }

       void revive_player()
            {
                  player.revive();
             }

        bool player_destroyed() const
            {
                  return player.is_destroyed();
            }
};


int main()
{
   Player pl("Johnson");

   if(!pl.player_destroyed())
       std::cout<<"Player not destroyed\n";
  
    pl.destroy_player();
  
    if(pl.player_destroyed())
       std::cout<<"player got destroyed\n";

   pl.revive_player();
   
    if(!pl.player_destroyed())
       std::cout<<"player revived\n";
}
Last edited on
@andy1992,
I know the method you posted.
Just wanted to know that my code is also correct or not .
And if it is correct, are there any issues with that approach?
Just wanted to know that my code is also correct or not .
It's not correct. you cannot assign a dynamically created object this way.
The best would be a destroy function like so:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
class Player
{
        void destroy()
{
 name.clear();
 delete pointer_to_xyz; // Note: nullptr or allocated by this class
 pointer_to_xyz = nullptr;
}
};
...
class Square
{
private :
	Player player;

public :
	Square(Player p)
	{
		player = p;
	}

	void destroy()
	{
		player.destroy();
	}
};
closed account (SECMoG1T)
Surely there are some issues.

1. player is not a pointer
player = new Player("" , new XYZ())///dont call XYZ constructor like that

2. There might be some memory leak.

3. Your function doesn't destroy anything, it allocates a new object instead so how did
you take care of the initial object.

i'll not recommend using dynamic memory coz you really have a lot to track but then there is an easier option-> smart pointers might be a little more friendly


@Coder i hadn't seen your post, i probably typed for sooooo long. :D
Last edited on
@andy1992 and @coder777,

extremely sorry , by destroy i didn't meant "destroy" .
I just intended to re-initialize the object to default value.
closed account (SECMoG1T)
I so you can make a function to reset the state of the objects

for example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
class Player
{
   public:
        void reset()
           {
               ///reset the variables you want
            }

    /// other members
  private:
   ///others
 };

int main()
{
   Player pl;
  pl.reset();///resets object to the default value
}


So the player is really an optional property of the Square. One way to represent this is with a unique_ptr:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <memory>
class Square
{
private :
	std::unique_ptr<Player> player;

public :
	Square(Player p)
	{
		player = new Player(p);
	}

	void destroy()
	{
		player.reset();
	}
};

Now the square is associated with a player if player is non-null. Another way is to use a sentinel value:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
class Square
{
private :
	Player player;
	const char *unoccupied_name = "ebita ebita ebita no player";
public :
	Square(Player p)
	{
		player = p;
	}

	void destroy()
	{
		player.name = unoccupied_name;
	}
};

In both cases you have to check if the player is valid before using it. The unique_ptr method is nice because the check is fast.
Topic archived. No new replies allowed.