C++ add virtual method in polymorphic subclass

By : vard
Source: Stackoverflow.com
Question!

I have cumbersome class and I want to refactor it to replace type code with subclasses. At some point during such process I have following hierarchy:

// interface
ISomeClass(){
public:
    virtual foo() = 0;
    virtual ~ISomeClass();
}


// this class is cumbersome one with huge amount of conditional logic based on type
BaseSomeClass : public ISomeClass(){
public:
    virtual foo(){
       if(TYPE_0 == getType()){   // finally I want to move such conditional logic in subclass
           doSmth();
       } else if (TYPE_1 == getType()){
           doAnother();
       }
    }


protected:
    virtual int getType(){     // I temporary need it for refactoring issue 
        return type_;          // to replace type_ with subclasses
    }

private:
    int type_;

};

// this classes is almost empty now, but I want to move there all conditional logic in future
class Implementation1 : public BaseSomeClass {
     virtual int getType(){    // I temporary need it for refactoring issue 
        return TYPE_0;         // to replace type_ with subclasses
    }
}; 


class Implementation2 : public BaseSomeClass {
     virtual int getType(){    // I temporary need it for refactoring issue 
        return TYPE_1;         // to replace type_ with subclasses
    }
};

In BaseSomeClassdefined additional virtual method getType(). Would this method behavior be polymorphic if I handle all the instances using some kind of interface ISomeClass pointer? Assuming the interface itself doesn't provide such virtual method. Please notice this code is a first step in refactoring, not final one. Also this is a simplified example and real code has tens of such methods, I need to do refactoring step by step. And the question is about C++ dynamic polymorphism.

By : vard


This video can help you solving your question :)
By: admin