Some Comments on Quiz 3
1. The class Box in quiz 3 is a class in canonical form. A class in canonical
form has a copy constructor, a destructor and overloaded operator =
defined. These three are needed where the class has dynamically allocated
memory.
2. Destructor
Many of you wrote the following in the code for the destructor
delete *value;
Now value is a pointer to an integer. Therefore *value is an integer.
delete is always applied to a pointer. And here delete is being applied to
an integer. Doesn't make sense.
3. The copy constructor
There were two main mistakes in this part. First,
value = other.value
Now the reason for having a copy constructor is that whenever copy of an
object is created, deep copying (refer lecture notes to see what deep
copying is) is carried out.
What the above statement does is shallow copy.
Also, there were some people returing values from a constructor. Note, that
the constructor does not return any values.
4. And then there were type errors of all sorts
*value = (other.value);
value = *other.value;
5. 'this' is a pointer to an object and not object itself. So, saying
this.value does not make much sense. Instead what you should be saying is
this->value
6. Consider the following declarations
Box b;
Box *b1;
b1 = new Box;
now which of the following wont generate any compilation error
b.value
b1.value
b->value
b1->value
(*b).value
(*b1).value