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

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; }

13th Feb 2019, 9:40 PM
JanSeliv - avatar
1 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
22nd Mar 2020, 6:04 AM
Eduard Arias
Eduard Arias - avatar