class NearestNeighbour { private: DistanceBase* _d; public: ... NearestNeighbour() : _d(NULL) {} void distance(const DistanceBase& d) { _d = d.clone(); } DistanceBase& distance(); // Checks _d is valid int nearest(const RD_Array<RD_Vector>& data, const RD_Vector& v); // Uses distance().distance(dv) };
Implementation: Return error if object not set up.
DistanceBase& NearestNeighbour::distance() { if (_d) return *_d; else { cerr<<"NearestNeighbour::distance() : "; cerr<<"No object defined."<<endl; abort(); } }