Converting child class to parent | Sololearn: Learn to code for FREE!


Converting child class to parent

The function takes the parent class. So, why output "ChildException"? Only the parent function must be called, the parent object "BaseException" knows nothing about the overriding of the child function. Image demonstration: class ChildException : public exception{ public: ChildException() : exception("BaseException") {} const char* what() const final { return "ChildException"; } }; void foo(const exception& BaseException) { cout << BaseException.what() << endl; } int main() { foo(ChildException()); return 0; }

2/13/2019 9:40:23 PM


1 Answer

New Answer


You are overloading the what() method, so when you call that method it will always be the most "fresh" version of the function. To understand better that concept I invite you to read about polymorphism