C ++ throw取消引用的指针 [英] C++ throw dereferenced pointer

查看:183
本文介绍了C ++ throw取消引用的指针的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面抛出的异常对象的类型是什么:



问题1> range_error r(error); throw r;



Answer1> range_error的对象



Question2> exception * p =& r;



问题3>

exception * p =& r; throw p;



Answer3>抛出一个指向range_error的指针。捕获处理可以通过动态绑定访问range_error成员函数。



我能得到这些问题吗?



//在VS2010上更新和编译并运行

  #include< iostream> 
using namespace std;

class ExClassA
{
public:
virtual void PrintMe()const
{
cout< ExClassA< endl;
}
};

class ExClassB:public ExClassA
{
public:
virtual void PrintMe()const
{
cout< ExClassB< endl;
}
};

int main(int argc,char * argv [])
{
ExClassB exClassB;
ExClassA * p =& exClassB;

try
{
throw * p;
}
catch(const ExClassA& e)
{
e.PrintMe();
}

try
{
throw p;
}
catch(const ExClassA * e)
{
e-> PrintMe();
}
}

上述程序的第一个try-catch打印ExClassA



上述程序的第二个try-catch打印ExClassB

解决方案

我想你三个都是对的。抛出对象(IIRC)的类型是要抛出的对象的静态类型。我将不得不深入的标准一会儿找到确切的报价,但一个简单的例子似乎证实了这一点:

  struct base {}; 
struct derived:base {};
void t(){
derived d;
base * b =& d;
throw * b;
}
int main(){
try {
t();
} catch(derived const&){
std :: cout<< derived<< std :: endl;
} catch(base const&){
std :: cout<< base<< std :: endl;
}
}

如果正在抛出的对象的动态类型,则 * b 将具有类型和第一个 catch 将成功,但根据经验,第二个 catch 被执行(g ++)。



throwwn是指向 range_error 对象的 exception 的指针。稍微不同的是可以捕获的,编译器不会捕获在 catch(range_error *)块。答案是正确的,但我会指定指针的类型,就像pointee的类型。 (指针的类型在某种程度上隐含在答案中)


What is the type of the exception object in the following thrown:

Question1> range_error r("error"); throw r;

Answer1> an object of range_error

Question2> exception *p = &r; throw *p;

Answer2> a sliced object of exception

Question3> exception *p = &r; throw p;

Answer3> a pointer pointing to range_error is thrown. The capture-handling can access the range_error member functions through dynamic binding.

Do I get these question right?

// Updated and Compiled and Run on VS2010

#include <iostream>
using namespace std;

class ExClassA
{
public:
    virtual void PrintMe() const
    {
        cout << "ExClassA" << endl;
    }
};

class ExClassB : public ExClassA
{
public:
    virtual void PrintMe() const
    {
        cout << "ExClassB" << endl;
    }
};

int main(int argc, char* argv[])
{   
    ExClassB exClassB;
    ExClassA *p = &exClassB;

    try
    {
        throw *p;
    }
    catch (const ExClassA& e)
    {
        e.PrintMe();        
    }

    try
    {
        throw p;
    }
    catch (const ExClassA* e)
    {
        e->PrintMe();
    }
}

The first try-catch of above program prints "ExClassA"

The second try-catch of above program prints "ExClassB"

解决方案

I think you are right in all three. The type of the thrown object (IIRC) is the static type of the object being thrown. I would have to dig into the standard for a while to find the exact quotes, but a simple example seems to confirm this:

struct base {};
struct derived : base {};
void t() {
    derived d;
    base * b = &d;
    throw *b;
}
int main() {
    try {
        t();
    } catch ( derived const & ) {
        std::cout << "derived" << std::endl;
    } catch ( base const & ) {
        std::cout << "base" << std::endl;
    }
}

If the dynamic type of the object being thrown was used, then *b would have type derived and the first catch would succeed, but empirically the second catch is executed (g++).

In the last case, the object thrown is a pointer to exception that refers to a range_error object. The slight difference is again what can be caught, the compiler will not catch in a catch (range_error*) block. The answer is correct, but I would have specified the type of the pointer, as much as the type of the pointee. (The type of the pointer is somehow implicit in the answer)

这篇关于C ++ throw取消引用的指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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