next up previous
Next: Default constructors Up: The Strategy Pattern Previous: clone()

Using clone()

class NearestNeighbour
{
private:
    DistanceBase* _d;
public:
    NearestNeighbour(const DistanceBase& d)
        { _d = d.clone(); }
    ~NearestNeighbour()
        { delete _d; }
...
};
Note: We must also write explicit operator=() and copy constructors.

Implementation of copy and copy constructor

NearestNeighbour::NearestNeighbour(const NearestNeighbour& n)
    : _d(NULL)
{
    *this = n;   // Uses operator=()
}

NearestNeighbour& 
NearestNeighbour::operator=(const NearestNeighbour& nn)
{
    delete _d; _d=NULL;
    if (nn._d) _d = nn._d->clone();
    return *this;
}



Tim Cootes
2/24/1998