为什么会出现从指针隐式类型转换在C ++中为bool? [英] Why is there an implicit type conversion from pointers to bool in C++?
本文介绍了为什么会出现从指针隐式类型转换在C ++中为bool?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
考虑级富
像这样定义了两个构造函数:
Consider the class foo
with two constructors defined like this:
class foo
{
public:
foo(const std::string& filename) {std::cout << "ctor 1" << std::endl;}
foo(const bool some_flag = false) {std::cout << "ctor 2" << std::endl;}
};
实例化一个字符串类,并猜测其构造函数被调用?
Instantiate the class with a string literal, and guess which constructor is called?
foo a ("/path/to/file");
输出:
男星2
我不知道你怎么想,但我不发现,在节目历史上最直观的表现。我敢打赌,有一些聪明的原因吧,不过,我想知道是什么可能?
I don't know about you, but I don't find that the most intuitive behavior in programming history. I bet there is some clever reason for it, though, and I'd like to know what that might be?
推荐答案
这是很常见的用C写这个
It's very common in C to write this
void f(T* ptr) {
if (ptr) {
// ptr is not NULL
}
}
您应该做一个为const char *
构造。
这篇关于为什么会出现从指针隐式类型转换在C ++中为bool?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文