When searching for the answer to this request with the recording of test code, I found out that private / protected inheritance changes the way of getting exceptions from different classes, which was very surprising. To find the answer, I mentioned the forum questions earlier, and I met this similar question .
Itβs quite obvious to me to use protected inheritance for a base class with virtual methods. Keeping the standard aside, I wanted to know why in C ++ exception handling is limited to inheritance when virtual method calls are not ? The following snippet explains this:
struct Base { virtual void printError () = 0; }; class Derived : protected Base { void printError () { } }; int main () { try { throw new Derived; } catch(Base *p) { p->printError(); }
Edit: If we look at privacy as an answer; received. But why is this only applicable to catch() receiving base pointers, while it is not applicable to functions receiving base pointers?
iammilind
source share