C++ 如何在使用g+抛出异常时处理多重继承+;(4.7.1以后)是否具有O1或更多优化?

C++ 如何在使用g+抛出异常时处理多重继承+;(4.7.1以后)是否具有O1或更多优化?,c++,gcc,exception-handling,g++,gcc4.8,C++,Gcc,Exception Handling,G++,Gcc4.8,我使用几个GCC尝试了以下代码,但在运行使用O1和gcc4.7.1及更高版本编译的代码时遇到了问题 #include <stdexcept> #include <iostream> struct Interface_1 { virtual void setA() = 0; }; struct Interface_2 { virtual void setB() = 0; }; struct Master: Interface_1, Interface_

我使用几个GCC尝试了以下代码,但在运行使用O1和gcc4.7.1及更高版本编译的代码时遇到了问题

#include <stdexcept>
#include <iostream>

struct Interface_1
{
    virtual void setA() = 0;
};

struct Interface_2
{
    virtual void setB() = 0;
};

struct Master: Interface_1, Interface_2
{
    void setA()
    {
        throw std::runtime_error("I was thrown");
    }
    void setB()
    {
        throw std::runtime_error("I was thrown");
    }
};

int main()
{
    Master m;
    Interface_2& i2 = m;
    try
    {
        i2.setB();
    }
    catch (const std::runtime_error& e)
    {
        std::cout << "Caught exception e=" << e.what() << std::endl;
    }
    catch (...)
    {
        std::cout << "Did not catch exception" << std::endl;
    }

    std::cout << "Done running" << std::endl;
}
但如果我使用接口_1中的引用,它就可以工作:

int main()
{
    Master m;
    Interface_1& i1 = m;
    try
    {
        i1.setA();
    }
    catch (const Exception& e)
    {
        std::cout << "Caught exception e=" << e.what() << std::endl;
    }
    catch (...)
    {
        std::cout << "Did not catchexception" << std::endl;
    }

    std::cout << "Done running" << std::endl;
}
我从中继承的第一个接口似乎只能用于引用主类中的对象。我还从第一个接口进行了如下继承:

class Master: public virtual Interface_1, public Interface_2
然后来自接口_2的引用被证明是好的。相反,我对接口1的引用有问题

我在4.9之前的g++中尝试过这个,我不知道它是否在5.x版本中工作。
不幸的是,我不能去更改编译器的版本。由于限制和限制,我应该坚持使用4.8。因此,我想知道你们是否知道解决这个问题的其他/聪明的方法。

让析构函数虚拟化有帮助吗?@Galik-有没有理由认为这很重要,或者这只是迷信的编程?这不是问题所在,但除非你需要额外的东西,否则不要使用
std::endl
<代码>“\n”结束一行。@Galik:为了它的价值。不,它没有帮助。仅供参考,使用AppleClang 7.0.2.7000181时,此功能可以正常工作。
Caught exception e=I was thrown
Done running
class Master: public virtual Interface_1, public Interface_2