我想使用 bool cast 重载检查某些内容是否有效:
Menu::operator bool() const {
bool empty = false;
if (m_title == nullptr) {
empty = true;
}
return empty;
}
Run Code Online (Sandbox Code Playgroud)
但是当我使用
if (Menu1) { cout << "valid"; }
Run Code Online (Sandbox Code Playgroud)
它使用 int cast 重载代替
Menu::operator int()
{
int choice = option();
return choice;
}
Run Code Online (Sandbox Code Playgroud) c++ class constants operator-overloading conversion-operator
简单地说,我正在通过if语句检查两个 char* 是否为 nullptr 或为空,但我收到一条警告,说我正在取消引用一个空指针。
// mplate is a reference to a class
if ((mplate.m_plate != nullptr || mplate.m_plate[0] != '\0') || (plate != nullptr || plate[0] != '\0')) {
// Do something
}
else {
// do something else
}
Run Code Online (Sandbox Code Playgroud)
所以基本上我想在if声明中说的是,如果要么mplate.mplate或plate为空,要么nullptr这样做,否则做其他事情。
Severity Code Description Project File Line Suppression State
Warning C6011 Dereferencing NULL pointer 'make'.
Warning C6011 Dereferencing NULL pointer 'model'.
Warning C6011 Dereferencing NULL pointer 'mplate.m_plate'.
Warning C6011 …Run Code Online (Sandbox Code Playgroud)