How do I delete map<char, map<char, char>> board_game
, map<int, char> X_index
and list<Node*>
?
How would I write a proper destructor for the classes Tree
, Node
and Boardgame
?
class Search_tree {
Node* root = nullptr;
char playerColor;
list<Node*> leaveNodes;
// Constructor, functions ...
~ Search_tree(); <--?
}
class Node {
Board_game* currentBoard = nullptr;
Node* parent = nullptr;
list<Node*> childs;
// Constructor, functions ...
~Node(); <--?
}
class Board_game {
public:
// Boardgame
map<char, map<char, char>> board_game;
map<int, char> X_index;
map<int, char> Y_index;
// Figures
Position figures[8];
// Constructor, functions ...
~Board_game(); <--?
}
struct Position {
char x;
char y;
};
CodePudding user response:
You need to decide who owns what. Then you should turn those "owning" raw pointers into std::unique_ptr
and then you won't need hand-written destructors. A possible ownership structure might be:
class Search_tree {
std::unique_ptr<Node> root;
char playerColor;
list<Node*> leaveNodes;
}
class Node {
Board_game* currentBoard = nullptr;
Node* parent = nullptr;
list<std::unique_ptr<Node>> childs;
}
Since Board_game
does not contain any raw pointers, it already doesn't need a hand-written destructor.