私有继承,朋友和异常处理 [英] private inheritance, friends, and exception-handling

查看:159
本文介绍了私有继承,朋友和异常处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当A类私有地从B类继承时,它意味着B是A的私有基类子对象。但不是对于朋友,对于朋友而言,它是公共子对象。当有多个catch处理程序时,第一个匹配(即,如果异常类型可以隐式转换为处理程序的参数类型)被调用。那么有人会向我解释为什么下面的代码不能像我期望的那样工作吗?标准是出于此行为还是MSVC错误?

When class A privately inherits from class B it means that B is a private base class subobject of A. But not for friends, for friends it is a public sububject. And when there are multiple catch handlers the first one that matches (that is, if the exception type can be implicitly converted to the handler's parameter type) is called. So will anyone explain to me why the following code does not work as I expect? Is this behavior intended by the standard or is this a MSVC bug?

class A
{
};
class B:A //private inheritance 
{
    friend void g();
}; 

void f()
{

    B b;
    //A* pa = &b; // error, conversion exists, but is inaccessible
    throw b;
}

void g()
{
    B b;
    A* pa = &b; //ok, private inheritance, but g() is B's friend so it is as though public
    try
    {
        f();
    }
    catch(A&)
    {
        //WHY ISN'T THIS HANDLER INVOKED?! B&->A& conversion exists in this function
    }
    catch(B&)
    {       
    }
}

int main()
{
    g();
}

P.S。这不是真正的代码,这是一个理论上的实验,也就是说,不要告诉我像朋友那样糟糕的东西,而且构图优于私人继承等。

P.S. This is NOT real code, this is a theoretical experiment, that is, don't tell me stuff like friends are bad and composition is superior to private inheritance etc.

谢谢提前

推荐答案

不,这不是标准所说的。它说(C ++ 0x):

Nope, that's not what the standard says. It says (C++0x):


处理程序是E类型的异常
对象的匹配,如果

A handler is a match for an exception object of type E if

- 处理程序的类型为cv T或cv T&
和E和T是相同的类型
(忽略顶级cv-quali firs),

— The handler is of type cv T or cv T& and E and T are the same type (ignoring the top-level cv-qualifiers), or

- 处理程序属于cv T或cv T&
和T是一个明确的公共基础
的E,或者

— the handler is of type cv T or cv T& and T is an unambiguous public base class of E, or

- 处理程序的类型为cv1 T * cv2
和E是一个指针类型,可以通过其中一个或两个将
转换为处理程序
的类型

— the handler is of type cv1 T* cv2 and E is a pointer type that can be converted to the type of the handler by either or both of

     - 标准指针转换(4.10)
不涉及转换为指针
转为私有或受保护
或含糊不清的

    — a standard pointer conversion (4.10) not involving conversions to pointers to private or protected or ambiguous classes

     - 资格转换

    — a qualification conversion

- 处理程序是指向$ b的指针或指针$ b成员类型,E是std :: nullptr_t

— the handler is a pointer or pointer to member type and E is std::nullptr_t

理由:复杂的实施。你可以看一下这个转换发生在throw和catch之间,而不是g本身。

Rationale: Complicated implementation. You can look at this like the conversion happens somewhere between the throw and the catch, not in g itself.

这篇关于私有继承,朋友和异常处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆