【发布时间】:2011-06-15 15:07:26
【问题描述】:
在我的代码中,有一个错字:在初始化std::string 对象时,我没有使用"false",而是输入了false(这是一个bool)。现在这并没有报告任何编译错误。但稍后在我的代码中,当使用这个字符串对象时,我在运行时得到std::logic_error。谁能解释一下,为什么在这种情况下允许构造(否则我会收到编译错误并在那里发现问题)?
这是一个小sn-p-
#include <iostream>
int main ()
{
std::string str = false;
std::cout << str << "\n";
}
运行时得到的 o/p -
xhdrdevl8@~/MYBACKUP=>g++ -o test_string -g test_string.cxx
xhdrdevl8@~/MYBACKUP=>./test_string
terminate called after throwing an instance of 'std::logic_error'
what(): basic_string::_S_construct NULL not valid
Aborted
【问题讨论】:
标签: c++ string constructor type-conversion