I've just started the Copy Control chapter in C++ Primer (5th Edition) and stumbled upon a vague exercise, which is as follows:
Given the following classes, implement a default constructor and the necessary copy-control members.
1 2 3 4 5 6 7 8 9 10 11 12
|
class TreeNode {
private:
std::string value;
int count;
TreeNode* left;
TreeNode* right;
};
class BinStrTree {
private:
TreeNode* root;
};
| |
|
The default constructor is simple but I don't quite grasp the use of copy-control members here, so I have a few questions:
1. What is the use of
count
? It doesn't make sense to use it as a reference count, since it's an independent data member.
2. What happens when a destructor gets called on
TreeNode
?
value
gets its' own destructor called,
count
is of a built-in data type so it's left alone, but what about
left
and
right
? Seeing as this is a binary search tree (not too familiar with those), shouldn't the other members of said tree be left untouched or simply reconnected rather than
delete
d?
3. The copy-constructor is relatively straightforward: copy over all members. But that doesn't make sense either. If I copy over
left
and/or
right
, how should the program differentiate between the two
TreeNode
s? Should this even support a copy-constructor and a copy-assignment operator?