<<< Virtual functions     Index     Using virtual functions >>>

26. Possible alternative: Faking it

  • 
    class Shape {
    public:
        enum EnumType {
            Circle, Square
        };
    
        Shape( EnumType type );
        void draw();
    
    private:
        EnumType m_shape_type;
    
    };//class Shape
    
    
    void Shape::draw()
    {
        switch ( m_shape_type ) {
        case Circle:
            //draw a circle...
    
        case Square:
            //draw a square...
        }
    }
    
    
  • So why not fake it?

    1. Can't extend hierarchy without disturbing the base class.

      • This makes maintenance difficult.

    2. All functionality is in one type, so specific functionality has no home:

      • e.g. where do we put the radius( ) function?

    3. Faking is almost guaranteed to be slower!

<<< Virtual functions     Index     Using virtual functions >>>