C++ Exceptions and Inheritance from std::exception

泄露秘密 提交于 2019-11-30 17:59:32

When you inherit privately, you cannot convert to or otherwise access that base class outside of the class. Since you asked for something from the standard:

§11.2/4:
A base class is said to be accessible if an invented public member of the base class is accessible. If a base class is accessible, one can implicitly convert a pointer to a derived class to a pointer to that base class (4.10, 4.11).

Simply put, to anything outside the class it's like you never inherited from std::exception, because it's private. Ergo, it will not be able to be caught in the std::exception& clause, since no conversion exists.

Could someone please explain to me why the type of inheritance matters in this case? Bonus points for a reference in the standard.

The type of inheritance doesn't matter. It only matters that you have an accessible conversion available to one of the catch types. It just so happens that since it is not public inheritance there is no public accessible conversion.


Explanation:

You can see the same behavior here:

class B
{
};

class C1 : B
{
};

class C2 : public B
{
};

int main(int argc, char** argv)
{
    B& b1 = C1();//Compiling error due to conversion exists but is inaccessible
    B& b2 = C2();//OK
    return 0;
}

A thrown exception is only caught by a catch block if:

  1. The catch block has a matching type, or
  2. The catch block is for a type that has an accessible conversion
  3. The catch block is a catch(...)
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!