+ 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: https://drive.google.com/open?id=1HwJnchJqnxV0ZEcum4hatBYGTys_LaVU 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
JanSeliv - avatar
1 Resposta
0
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