C++ 中的 if 语句中的接口对象

Object of an interface inside if-statement in C++

本文关键字:接口 对象 语句 中的 if C++      更新时间:2023-10-16

你好,我有一个简短的问题。 由于我对C++不太熟悉,并且我现在正在浏览一些代码,我想知道类似于这个函数的函数中发生了什么:

#include "SomeInterface.h"
class SomeClass {
public:
...
void SomeFunction(...){
if (interface) {   //What is asked here exactly?
/*do something*/
}
}
/*...*/
protected:
SomeInterface* interface;
}

由于我来自Java,我想知道if括号中问了什么。 我习惯于只有在变量为 bool 类型时才能在 if 括号中放置一个孤独的变量名称。但是由于这里是自定义界面类型,我很困惑。

提前感谢您的回答。

既然interface是一个指针,那么

if (interface)

相当于

if (interface != nullptr)

指针可隐式转换为boolnullptrfalse,其他任何内容都将true