C在struct中重载operator()

C在struct中重载operator(),第1张

概述这是来自Box2d物理引擎的b2Math.h的代码. struct b2Vec2{ ... /// Read from and indexed element. float operator () (int i) const { return (&x)[i]; } /// Write to an indexed element. 这是来自Box2d物理引擎的b2Math.h的代码.

struct b2Vec2{   ...    /// Read from and indexed element.    float operator () (int i) const    {         return (&x)[i];    }    /// Write to an indexed element.    float operator () (int i)    {         return (&x)[i];    }    ...    float x,y;}

为什么我们不能只使用SomeVector.x和SomeVector.y来读/写矢量坐标?实际上如何返回(& x)[i];作品?我的意思是,在结构的x组件引用之后的数组brakets []对我来说并不清楚.

在此先感谢您的回复.

解决方法

Here is a code from b2Math.h from Box2d physics engine.

您发布的Box2D源代码的复制和粘贴似乎有错误.特别是,在非const方法中似乎缺少了&符号.

此外,此代码段似乎来自除当前2.3.2发布代码之外的代码库.

这是Box2D 2.3.2 sources on GitHub的部分:

/// Read from and indexed element.float32 operator () (int32 i) const{    return (&x)[i];}/// Write to an indexed element.float32& operator () (int32 i){    return (&x)[i];}

Why can’t we just use SomeVector.x and SomeVector.y to read/write vector coordinates?

我们可以,而且我们通常这样做.

然而,Box2D中的一些代码(具体为b2AABB :: RayCast)似乎是从算法编写的(b2AABB :: RayCast的评论说“来自实时碰撞检测,p179”)迭代x和y为数组下标零和一.我猜想Erin Cato(Box2D的作者)以这种方式实现了这些运算符:(a)使风格上的访问更符合算法,(b)使其工作,(c)使其工作在看似高效的方式.我可以确认它至少起作用了.

我有my own fork of Box2D,我已经重写了这些运算符.我将其接口更改为使用[](而不是()),并将其实现更改为使用switch语句显式访问x或y.后者我明确地避免了我可能未定义的行为w.r.t. C标准.

这里是我的实现的相关部分的代码段(请注意,我并不认为这是完美或好的,只是它的替代实现是有效的,它应该明确依赖于定义的行为) :

/// Accesses element by index./// @param i Index (0 for x,1 for y).auto operator[] (size_type i) const{    assert(i < max_size());    switch (i)    {        case 0: return x;        case 1: return y;        default: break;    }    return x;}/// Accesses element by index./// @param i Index (0 for x,1 for y).auto& operator[] (size_type i){    assert(i < max_size());    switch (i)    {        case 0: return x;        case 1: return y;        default: break;    }    return x;}

And how actually line return (&x)[i]; works?

正如我上面暗示的那样,我之前已经研究过这个问题.

当结构的x和y成员变量的内存布局与具有两个浮点数的数组相同时,它可以工作;它通常做的.因此,&符号(&)获取x参数的地址,然后将该地址视为数据开头的地址,然后由i进行索引.

然而,就C标准而言,我不认为这是定义的行为.然而,根据我的口味,它没有明确定义.

希望这能回答你的问题.

总结

以上是内存溢出为你收集整理的C在struct中重载operator()全部内容,希望文章能够帮你解决C在struct中重载operator()所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/1225034.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-05
下一篇 2022-06-05

发表评论

登录后才能评论

评论列表(0条)

保存