【发布时间】:2021-08-01 01:09:20
【问题描述】:
class D{
bool var;
public:
D(bool x): var(x) {}
operator bool(){return var;}
};
int main() {
D* temp1 = new D(false);
cout << *temp1; //0
D* temp2 = new D(true);
cout << *temp2; //1
return 0;
}
我正在尝试重载对象 D 的 bool 转换。然后我发现重载的 bool 转换与取消引用对象指针时的值之间存在关系。
我用不同的值初始化了 2 个对象。当我尝试取消引用指针时,我看到它们返回与重载 bool 转换相同的值。 重载bool转换和解引用对象指针是什么关系?
【问题讨论】: