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

2/13/2019 9:40:23 PM

JanSeliv

1 Answer

New Answer

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