I have a question regarding to the C++ virtual function.
The derived class (DerivedAlgo) implements the virtual function (BaseAlgo::process) but changes a little bit in the function signature by using the DerivedData type as the input parameter instead of BaseData.
Despite the pure abstract function in the base class, I am still able to create an instance of the DerivedAlgo class and the compiler does not complain at all.
I just wonder anyone knows any C++ rule which can explain the following code.
class BaseData{}; class DerivedData: public BaseData{}; class BaseAlgo{ public: virtual void process(BaseData& data) = 0; }; class DerivedAlgo: BaseAlgo{ public: virtual void process(DerivedData& data){ std::cout << "hello world!" << std::endl; } }; In my another sample code, I define process(DerivedData&) and process(BaseData&).
The compiler can still run through without complaining any ambiguity.
class DerivedAlgo{ public: virtual void process(DerivedData& data){...} virtual void process(BaseData& data){...} }; BaseData baseData; DerivedData derivedData; derivedAlgo.process(baseData); derivedAlgo.process(derivedData); I really appreciate any of your inputs. Thanks!