C ++无法正确打印字符指针,显示其他字符

时间:2019-07-15 20:47:32

标签: c++ exception char

我有以下自定义的NotImplementedException类。

#include <stdexcept>

#ifndef NOT_IMPLEMENTED_EXCEPTION_H
#define NOT_IMPLEMENTED_EXCEPTION_H

class NotImplementedException : public std::logic_error {

private:

    std::string m_message, m_function;

public:

    NotImplementedException(const char* function);
    ~NotImplementedException();

    virtual const char* what() const;

};

#endif


#include "NotImplementedException.h"

NotImplementedException::NotImplementedException(const char* function) : 
std::logic_error("NotImplementedException"), m_message("Function not 
implemented: "), m_function(function) {}

NotImplementedException::~NotImplementedException() {}

const char* NotImplementedException::what() const {

    std::logic_error::what();
    return (m_message + m_function).c_str();

}

我在以下函数中利用了此异常。

virtual double Delta() const {
    throw NotImplementedException(__FUNCTION__);
}

这是主班的考试:

try {

    std::cout << option.Delta() << std::endl;

}
catch (std::logic_error& ex) {
    std::cout << ex.what() << std::endl;
}

从控制台打印: enter image description here

我不明白为什么它会从what()这样打印消息。例如,如果我将函数签名更改为std :: string what(),则消息将正确打印出来。我打印出的字符*不正确吗?

0 个答案:

没有答案
相关问题