CSCI 2270
Computer Science 2:
Data Structures
Fall 2003
Karl Winklmann
Tree copy constructor, assignment operator, destructor
Tuesday, October 21, 2003
While not needed in Assignment 4, a robust and generally usable
tree class would have the usual set of three pieces
of any class involving dynamically allocated memory:
a copy constructor, an assignment operator, and a destructor.
A copy constructor
As usual, once we have an assignment operator, the copy
constructor is easy to write: initialize the object, then
assign to it.
An assignment operator
Assignment is done by assigning
the name and size variables
and constructing the children. This relies on the
copy constructor which --- see above --- relies on the
assignment operator, meaning that there is recursion
involving a function and an operator.
A destructor
The secret to success is to know that the C++
delete operator will first call your
destructor, then go ahead with the usual deletion of
dynamically allocated memory. This makes for very clean
and easy recursive code.
Here is the code
for all of the above.
Parse trees and derivatives
Here is the parsing demo
I used in class, in case you want to see the details.
|
© 2003 Karl Winklmann
|
3:08 PM, Tuesday, December 16, 2003
|