Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/162.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 检查对象是否是基于基类的子类的指令_C++_Pointers_Exception - Fatal编程技术网

C++ 检查对象是否是基于基类的子类的指令

C++ 检查对象是否是基于基类的子类的指令,c++,pointers,exception,C++,Pointers,Exception,我试图检查对象是否是某个类的实例,但它不起作用。以下是我的简化代码: class Base { public: Base() { } virtual ~Base() { } }; class Child : public Base { public: Child(int something) { } void Method() { throw Exception(this); } }; class Exception { pu

我试图检查对象是否是某个类的实例,但它不起作用。以下是我的简化代码:

class Base
{
public:
    Base() { }
    virtual ~Base() { }
};

class Child : public Base
{
public:
    Child(int something) { }

    void Method()
    {
        throw Exception(this);
    }
};

class Exception
{
public:
    Base* subject;
    Exception(Base* base) : subject(base) { }
};

/* ---------------------------------------------------- */

try
{
    Child ch(1);
    ch.Method();
}
catch (Exception& ex)
{
    // the exception is thrown in Child class
    // therefore pointer to Child object is passed as an argument
    // to Exception's contructor so I'd expect folowing statement to be true
    // but it isn't

    if (Child *child = dynamic_cast<Child *>(ex.subject))
        std::cout << "subject of the exception is Child" << std::endl;
    else
        std::cout << "subject of the exception is just Base" << std::endl;
}
类基
{
公众:
Base(){}
虚拟~Base(){}
};
类子:公共基
{
公众:
Child(int某物){}
void方法()
{
抛出异常(this);
}
};
类异常
{
公众:
基础*科目;
异常(Base*Base):主题(Base){}
};
/* ---------------------------------------------------- */
尝试
{
儿童ch(1);
方法();
}
捕获(例外和例外)
{
//异常在子类中引发
//因此,指向子对象的指针作为参数传递
//对于异常的构造函数,我希望下面的语句是正确的
//但事实并非如此
if(Child*Child=dynamic_cast(例如主体))

要修复您的示例,请将对象的构造放在“try”块之前。如果对象析构函数在块内声明,则会调用它

Child ch(1);
try
{
    ch.Method();
}

要修复您的示例,请将对象的构造放在“try”块之前。如果在块内声明了对象析构函数,则会调用该析构函数

Child ch(1);
try
{
    ch.Method();
}

ex.subject
在catch块中无效,因为它已被破坏。因此它导致未定义的行为

在这里,我看到两种解决方案:

1)如果您只需要知道是哪个类导致了错误:

class Exception
{
public:
    std::string subject;
    Exception(const std::string &base) : subject(base) { }
};
儿童:

void Method()
{
    throw Exception("Child");
}
2)如果需要引发异常的对象:

class Exception
{
public:
    std::string subject;
    Exception(const std::string &base) : subject(base) { }
};
在try块之前创建子对象

Child ch(1);
try
{
    ch.Method();
}
catch (Exception& ex)
{
    // the exception is thrown in Child class
    // therefore pointer to Child object is passed as an argument
    // to Exception's contructor so I'd expect folowing statement to be true
    // but it isn't

    //Do something with ch
}

ex.subject
在catch块中无效,因为它已被破坏。因此它导致未定义的行为

在这里,我看到两种解决方案:

1)如果您只需要知道是哪个类导致了错误:

class Exception
{
public:
    std::string subject;
    Exception(const std::string &base) : subject(base) { }
};
儿童:

void Method()
{
    throw Exception("Child");
}
2)如果需要引发异常的对象:

class Exception
{
public:
    std::string subject;
    Exception(const std::string &base) : subject(base) { }
};
在try块之前创建子对象

Child ch(1);
try
{
    ch.Method();
}
catch (Exception& ex)
{
    // the exception is thrown in Child class
    // therefore pointer to Child object is passed as an argument
    // to Exception's contructor so I'd expect folowing statement to be true
    // but it isn't

    //Do something with ch
}

你为什么不直接写
if(dynamic_cast(例如subject))
ex.subject
catch
块中无效,因为它已被销毁。因此它导致未定义behavior@Danh我在这后面有更复杂的代码,我想使用
child
变量-这只是一个例子。为什么不直接编写
if(dynamic_cast(例如subject))
ex.subject
catch
块中无效,因为它已被销毁。因此它导致未定义behavior@Danh我在这后面有更复杂的代码,我想使用
child
变量-这只是一个例子。谢谢,但是(根据第二个选项)如果孩子的构造函数抛出异常怎么办?@Martineralecký您可以创建从构造函数抛出的第二个异常类。第二个异常基于我的第一个示例谢谢,但是(根据第二个选项)如果孩子的构造函数抛出异常怎么办?@Martineralecký您可以创建第二个异常类,该类从构造函数中抛出。第二个异常基于我的第一个示例