<<< Ways to use inheritance     Index     More on Inheritance and Polymorphism >>>

23. Inheritance of mix-ins

  • 
    #include <string>
    class ErrorKeeper : private std::string {
    public:
        std::string get_error() const
        { return *this; }
    
    protected:
        ErrorKeeper(){}
    
        void reset_error()
        { erase(); }
    
        void set_error( const char* err )
        { std::string::operator=( err ); }
    
        void set_error( std::string const& err )
        { std::string::operator=( err ); }
    
    };//class ErrorKeeper
    
    
  • 
    class Worker : public ErrorKeeper {
    public:
        void run() {
            reset_error();
            // ...
            set_error( "everything ok" );
        }
    };//class Worker
    
    
    #include <cassert>
    int main()
    {
        Worker main_thread;
        main_thread.run();
        assert(
            main_thread.get_error() == "everything ok"
            );
        return 0;
    }
    
    
<<< Ways to use inheritance     Index     More on Inheritance and Polymorphism >>>