如何使函数中的引用参数可选(可选缓冲区)
因此,我正在制作一个函数来检测两个精灵之间的碰撞,并希望添加两个缓冲区作为可选参数,以填充每个对象碰撞的侧面角度。
然而,这意味着两个 buff 参数必须是引用,并且每当调用函数时都必须存在,并且我不知道有任何方法可以进行默认引用。我该如何做到这一点?
这是实际功能:
bool CheckCollision(T* obj1, T* obj2, float& CollBuff1= new float, float& CollBuff2= new float);
我尝试使用“new”进行默认设置,但它不起作用。
So I'm making a function that detects collisions between two sprites, and would like to add two buffers as optional parameters to be filled with the angle of the sides of each objects collision.
However this means the two buff parameters must be references, and must be there whenever the function is called, and I am not aware of any way to make a default reference. How can I do this?
Here's the actual function:
bool CheckCollision(T* obj1, T* obj2, float& CollBuff1= new float, float& CollBuff2= new float);
I tried to make a default with 'new' but it didn't work.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您可以重载该函数。只需定义第二个包装函数,如下所示:
如果您愿意,还可以在仅提供一个浮点数时定义第二个包装函数。
You can overload the function. Just define a second wrapper function like so:
And define a second one for when only one float is provided, if you like.
使用 Boost.Optional,没有指针,也没有样板重载:
您可以通过执行
if (CollBuff1)
测试是否获得了参数,然后使用CollBuff1.get()
访问引用(显然,另一个也以同样的方式)。With Boost.Optional, no pointers and no boilerplate overloading:
You can test whether you've got the argument by doing
if (CollBuff1)
, and then access the reference withCollBuff1.get()
(and the other one the same way, obviously).另一种(而且非常丑陋)的方法是这样的:
或者在你的情况下
An alternative (and very ugly) way to do this would be this:
or in your case
@James McNellis 的一个现已删除(为什么?)的答案指出了正确的解决方案:你不这样做。
为此使用指针,因为这些缓冲区显然是外参数(即,它们用于“返回”附加值)并相应地调用它:
或者,更好的是,重载它:
尽管我仍然使用指针来指示这些缓冲区将被填满。
A now deleted (why?) answer from @James McNellis points out the correct solution: You don't.
Use pointers for this, as those buffers are obviously out-parameters (i.e., they're used to "return" additional values) and call it accordingly:
Or, even better, overload it:
Though I'd still use pointers to indicate that those buffers are going to be filled.
尝试为引用参数定义默认值时遇到的基本问题是非常量引用不能绑定到临时对象或文字。
事实上这是合法的:
但这是不可取的。函数中的代码无法判断它是否是使用默认参数调用的,因此您为自己创建了一个相当不愉快的资源处理问题:谁将
删除
该浮动,如果调用者确实传递了某些内容,该代码会做什么? bdonlan 的过载更可取。The basic problem you have in trying to define a default value for a reference parameter is that a non-const reference can't be bound to a temporary object or a literal.
In fact this is legal:
But it's rather inadvisable. The code in the function can't tell whether it was called with the default parameter or not, so you've created a rather unpleasant resource-handling problem for yourself: who is going to
delete
that float, and what will that code do in the case where the caller did pass something? bdonlan's overload is preferable.