返回字段值的结构,其语法与(例如)Int 所用的语法相同
我可能只是感到困惑,如果我知道得更好的话就不想这样做,但这里是:
当你有 int myInt = 5;
时,你可以通过以下方式访问该 int 的值:调用变量的名称:int myOtherInt = myInt + 1;
或 myInt ++;
。
我有一个结构,我想以这种方式表现,并返回现有的值类型。
简而言之,该结构体有一个 Func 字段,并提供该函数的结果;它还具有最小/最大字段,可用于限制输出,无论函数的原始结果如何。
Func<Foo, int> _func;
int _min;
int _max;
public MyStruct(Func<Foo, int> func, int min, int max) {...}
// Doesn't work
public int this
{
get { return Clamp(_min, _max, _func()); }
}
Clamp(min, max, val) {}
所以,我希望能够写:
var myStruct = new MyStruct((myFoo => myFoo.IntVal * 5), 1, 1000);
int myInt = myStruct + 5;
It's likely that I'm just confused and wouldn't want to do this if I knew better, but here goes:
When you have int myInt = 5;
you can access the value of that int just by calling the name of the variable: int myOtherInt = myInt + 1;
or myInt ++;
.
I have a struct that I want to behave this way, and return an existing value type.
In short, the struct has a Func field, and provides the result of that function; it also has Min/Max fields you use to clamp the output regardless of the raw result of the function.
Func<Foo, int> _func;
int _min;
int _max;
public MyStruct(Func<Foo, int> func, int min, int max) {...}
// Doesn't work
public int this
{
get { return Clamp(_min, _max, _func()); }
}
Clamp(min, max, val) {}
So, I want to be able to write:
var myStruct = new MyStruct((myFoo => myFoo.IntVal * 5), 1, 1000);
int myInt = myStruct + 5;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这里有两种选择供您选择。您可以定义隐式转换运算符,也可以定义每个您感兴趣的算术运算符。
如下使用隐式强制转换运算符的完整示例。不需要实现
this
属性。在本例中,
myStruct + 5
使用您已实现的重载运算符将您的结构隐式转换为整数。There are two options for you here. You can define an implicit casting operator, or you can define each of the arithmetic operators you're interested in.
Here's a full example of using the implicit cast operator. No
this
property needs to be implemented.In this case,
myStruct + 5
implicitly casts your struct into an integer, using the overloaded operator you've implemented.您应该检查隐式运算符:
http://msdn.microsoft.com/en- us/library/z5z9kes2(v=vs.71).aspx 它有局限性,但可以
让你走得更远。
一篇很好的文章,介绍了示例及其局限性之一:
http://www.markhneedham.com/blog/2009/ 02/22/c-隐式运算符/
You should check out the Implicit operator:
http://msdn.microsoft.com/en-us/library/z5z9kes2(v=vs.71).aspx it has limitations, but could
get you a long way.
A nice post that does into examples and one of it's limitations:
http://www.markhneedham.com/blog/2009/02/22/c-implicit-operator/
您可以通过实现公共静态隐式运算符 int(MyStruct astruct) 来实现此目的(请参阅http://msdn.microsoft.com/en-us/library/z5z9kes2(v=vs.71).aspx 的更多详细信息。)以及
运算符 +
(请参阅http://msdn.microsoft.com/en-us/library/aa288467(v=vs.71).aspx 获取教程)。这样,我认为
应该可以工作,为
myStruct + 5
调用operator +
,然后为myInt =
调用implicit operator int
代码>部分。You could do this by implementing
public static implicit operator int(MyStruct astruct)
(see http://msdn.microsoft.com/en-us/library/z5z9kes2(v=vs.71).aspx for more details.) and also anoperator +
(see http://msdn.microsoft.com/en-us/library/aa288467(v=vs.71).aspx for a tutorial).with that, I think
should work, calling
operator +
formyStruct + 5
and thenimplicit operator int
for themyInt =
part.为什么不直接重载
运算符 +(MyStruct a, int b)
教程:http://msdn.microsoft.com/en-us/library/aa288467(v=vs.71).aspx
Why not just overload the
operator +(MyStruct a, int b)
A tutorial: http://msdn.microsoft.com/en-us/library/aa288467(v=vs.71).aspx