gpt4 book ai didi

c++ - 如何重载结构的空运算符?

转载 作者:太空狗 更新时间:2023-10-29 23:49:19 24 4
gpt4 key购买 nike

我想重载一个函数来检查结构对象是否为空。

这是我的结构定义:

struct Bit128 {
unsigned __int64 H64;
unsigned __int64 L64;

bool operate(what should be here?)(const Bit128 other) {
return H64 > 0 || L64 > 0;
}
}

这是测试代码:

Bit128 bit128;
bit128.H64 = 0;
bit128.L64 = 0;
if (bit128)
// error
bit128.L64 = 1
if (!bit128)
// error

最佳答案

#include <cstdint>
struct Bit128
{
std::uint64_t H64;
std::uint64_t L64;
explicit operator bool () const {
return H64 > 0u || L64 > 0u;
}
};

关于c++ - 如何重载结构的空运算符?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42903954/

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