gpt4 book ai didi

c++ - 是否允许将指向对象的指针与 nullptr 进行比较?

转载 作者:太空狗 更新时间:2023-10-29 20:36:15 25 4
gpt4 key购买 nike

我已经声明并实现了两个类,LibraryBookLibrary 有一个私有(private)成员Book** books。这个指向指针的指针用于存储指向 Book 对象的指针。我还在 Library 类中重载了 += 运算符,如下所示:

Library& Library::operator+=(Book* addThisBook){
bool added = false;
int index = 0;

if(isFull()){
cout << "Library is full!" << endl;
}else{
//add book in first available space
while(!added && index<librarySize){
if(books[index] == nullptr){
books[index] = addThisBook;
added = true;
}
index++;
}
numBooks++;
}
return *this;
}

我的问题是关于 if 语句中的条件;是否允许将指向自定义类的指针与 nullptr 进行比较?

最佳答案

is it allowed to compare a pointer to a custom class to nullptr?

是的,任何类型的指针都可以与 nullptr 进行比较(不)相等。

关于c++ - 是否允许将指向对象的指针与 nullptr 进行比较?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39320039/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com