gpt4 book ai didi

operator-overloading - D 中的一元否定运算符重载

转载 作者:行者123 更新时间:2023-12-04 20:53:53 26 4
gpt4 key购买 nike

代码

struct test
{
private real value;

this(real value)
{
this.value = value;
}

bool opUnary(string op)() if (op == "!")
{
return !value;
}
}

void main()
{
test a = 123.12345;
bool b = !a;
}

编译错误
prog.d(19): Error: expression a of type test does not have a boolean value

http://ideone.com/Kec81

也在 dmd 2.053、2.054 上测试

我的代码有什么问题?

最佳答案

您不能重载 ! D 中的运算符 - 参见 http://www.d-programming-language.org/operatoroverloading.html#Unary用于可重载一元运算符的列表。在不知道您在做什么的情况下,很难提出解决方法,可能值得一看 alias this虽然 - http://www.d-programming-language.org/class.html#AliasThis .

关于operator-overloading - D 中的一元否定运算符重载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6974812/

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