English 中文(简体)
C++ 例外和继承:例外
原标题:C++ Exceptions and Inheritance from std::exception

鉴于这一样本代码:

#include <iostream>
#include <stdexcept>

class my_exception_t : std::exception
{
public:
    explicit my_exception_t()
    { }

    virtual const char* what() const throw()
    { return "Hello, world!"; }
};

int main()
{
    try
        { throw my_exception_t(); }
    catch (const std::exception& error)
        { std::cerr << "Exception: " << error.what() << std::endl; }
    catch (...)
        { std::cerr << "Exception: unknown" << std::endl; }

    return 0;
}

我获得以下产出:

Exception: unknown

Yet simply making the inheritance of my_exception_t from std::exception public, 我获得以下产出:

Exception: Hello, world!

请允许我向我解释为什么在这种情况下继承事项类型? 在标准中注明参考点。

最佳回答

当你私下继承时,你不能转换成或以其他方式进入该类。 由于你要求从标准中找到一些东西:

§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).

简言之,对于从<条码>中继承下来的那种类别之外的任何东西:除外,因为它是私人的。 Ergo公司将无法在std:Exgion&上登记。 条款,因为不存在转换。

问题回答

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

继承类型确实如此。 这只是你可以轻易转换成一种捕获类型的事项。 诚然,由于这不是公共继承,因此没有公众可以进入的转变。


<>Explanation:

你可以在此看到同样的行为:

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

如果:

  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(...)




相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签