【发布时间】:2011-05-18 00:13:39
【问题描述】:
在你看来,哪个构造函数会被调用?
class Element {
public:
Element(bool b = true, bool c = true);
Element(const std::string s, bool b = true, bool c = true);
};
...
Element element("something", true);
错了!第一个。
我必须从头重新开始 Stroustrup 的书吗?
我尝试不使用 const,但没有任何变化。
看起来 char* 看起来更像 bool 而不是 std::string。
有了这段代码,一切都很好:
Element element(string("something"), true);
编译器:Microsoft Visual C++ 2010
操作系统:Windows 7
【问题讨论】:
-
您似乎已经在编辑中回答了您自己的问题。这里有(另一个)真正的问题吗?
-
旁注:请不要按值传递
std::strings; const 引用在这里可能更有效。 -
tenfour:是的,我一开始使用的是 const 引用。我试图删除引用以查看情况是否有所改善。
-
Charles Bailey:好吧,我的答案不是。我想避免明确使用 string()。
标签: c++ string constructor boolean