由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Programming版 - 关于 exception 的一个问题
相关主题
C++ 用户定义exception的标准用法是什么?a string define question (c++)
strcat()C++一个string的小问题
C++ STL map find does not work ???一个popen加gzip的问题
请教一个C里面string copy的问题为啥gcc找不到类的构造函数?
呼叫THRUST等C语言牛牛,菜鸟级C语言指针问题请问以下代码有什么错误?
c++ string 一问这段C++代码有啥问题
Why does default exception use char *?请教:函数后面的 throw() 有意义么?
C++ Strategies and Tactics 书上一个问题求助Array in C
相关话题的讨论汇总
话题: exception话题: throw话题: const话题: string话题: char
进入Programming版参与讨论
1 (共1页)
r*********r
发帖数: 3195
1
中定义了 class exception:
class exception
{
public:
exception() throw() { }
virtual ~exception() throw();
virtual const char* what() const throw();
};
问题: 为什么 what() 返回一个 const char * 类型?
为什么不返回一个 string 呢? 可以返回一个 string 吗?
t****t
发帖数: 6806
2
what if string throw another exception?

【在 r*********r 的大作中提到】
: 中定义了 class exception:
: class exception
: {
: public:
: exception() throw() { }
: virtual ~exception() throw();
: virtual const char* what() const throw();
: };
: 问题: 为什么 what() 返回一个 const char * 类型?
: 为什么不返回一个 string 呢? 可以返回一个 string 吗?

r*********r
发帖数: 3195
3
但是 logic_error 和 runtime_error 都有 data member 是 string 类型.
也就是说你 throw 一个 runtime_error object 时, 要作 string 的 copy
construction,
这样也有可能 throw another exception
t****t
发帖数: 6806
4
well, the standard only says the interface (there's no obligation on data
members). implementation can do whatever they like to do...

【在 r*********r 的大作中提到】
: 但是 logic_error 和 runtime_error 都有 data member 是 string 类型.
: 也就是说你 throw 一个 runtime_error object 时, 要作 string 的 copy
: construction,
: 这样也有可能 throw another exception

r*********r
发帖数: 3195
5
well, the standard says logic_error's interface is like this:
class logic_error : public exception
{
public:
explicit logic_error(const string& __arg);
}
hard to imagine an implementation that can actually avoid copying __arg.
can you?
t****t
发帖数: 6806
6
no, i can't explain now. hehe.

【在 r*********r 的大作中提到】
: well, the standard says logic_error's interface is like this:
: class logic_error : public exception
: {
: public:
: explicit logic_error(const string& __arg);
: }
: hard to imagine an implementation that can actually avoid copying __arg.
: can you?
:

r*********r
发帖数: 3195
7
i came up with an idea: define a char * data member x;
and do the following in the constructor:
x = (char *)malloc(__arg.size());
strcpy(x, __arg.c_str());
and return x in what(), and call free(x) in the destructor.
maybe that works, at least it's guaranteed that no additional exception will
be thrown.
1 (共1页)
进入Programming版参与讨论
相关主题
Array in C呼叫THRUST等C语言牛牛,菜鸟级C语言指针问题
C++ Q15: throwc++ string 一问
一道编程题 - throw ExceptionsWhy does default exception use char *?
请帮忙看看这个字符函数的错误在哪里C++ Strategies and Tactics 书上一个问题求助
C++ 用户定义exception的标准用法是什么?a string define question (c++)
strcat()C++一个string的小问题
C++ STL map find does not work ???一个popen加gzip的问题
请教一个C里面string copy的问题为啥gcc找不到类的构造函数?
相关话题的讨论汇总
话题: exception话题: throw话题: const话题: string话题: char