Let's take a look at the following example. We have some base class that has virtual functions and must be initialized with reference to the std::ostream
object:
#include <boost/noncopyable.hpp> #include <sstream> class tasks_processor: boost::noncopyable { std::ostream& log_; protected: virtual void do_process() = 0; public: explicit tasks_processor(std::ostream& log) : log_(log) {} void process() { log_ << "Starting data processing"; do_process(); } };
We also have a derived class that has a std::ostream
object and implements the do_process()
function:
class fake_tasks_processor: public tasks_processor { std::ostringstream logger_; virtual void do_process() { logger_ << "Fake processor processed!"; } public: fake_tasks_processor() : tasks_processor(logger_) // Oops! logger_ does not exist here...