操作员&和*在类中的函数原型 [英] Operator & and * at function prototype in class

查看:104
本文介绍了操作员&和*在类中的函数原型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到类似这样的问题:

  class Sprite {
...
bool checkCollision(Sprite& spr);
...
};

所以,如果我有这个类,我可以这样做:

  ball.checkCollision(bar1); 

但是如果我将类更改为:

  class Sprite {
...
bool checkCollision(Sprite * spr);
...
};

我要这样做:

  ball.checkCollision(& bar1); 

那么,



谢谢。

解决方案

在这两种情况下,你实际上是传递 bar1 的地址(并且你不会复制这个值),因为两个指针(Sprite *)和引用(Sprite&)都有引用语义, case显式(你必须显式地解引用指针来操纵指向的对象,并且你必须显式地将对象的地址传递给指针参数),在第二种情况下是隐式的(当你操作引用时, 您正在操作对象本身,因此它们具有值语法,调用者的代码不使用&运算符显式传递指针。



所以,指针和引用之间的巨大差异是你可以对指针/引用变量做什么:指针变量本身可以被修改,所以它们可以被改变为指向别的东西,可以被NULL,增加,减少等,因此在指针(您可以直接使用变量名访问)和它指向的对象(您使用*运算符访问的对象)之间存在强烈的分离,或者,如果您想访问成员, - >快捷方式)。



引用,而不是旨在只是它们指向的对象的别名,不允许对引用本身进行更改:初始化



一般来说,在C ++中,引用优先于指针,因为它们是指向它们的对象。我说的动机和其他一些你可以在

在性能方面,它们应该是一样的,因为引用实际上是伪装的指针;仍然可能存在一些角落情况,其中编译器可以在代码使用引用而不是指针时更多地优化,因为参考被保证不改变它们隐藏的地址(即,从他们的生命的开始到结束他们总是指向同一个对象),所以在一些奇怪的情况下,你可以在使用引用的性能中获得一些东西,但是,使用引用的重点是关于良好的编程风格和可读性,而不是性能。 / p>

I'm having a problem with a class like this:

class Sprite {
    ...
        bool checkCollision(Sprite &spr);
    ...
};

So, if I have that class, I can do this:

ball.checkCollision(bar1);

But if I change the class to this:

class Sprite {
    ...
        bool checkCollision(Sprite* spr);
    ...
};

I have to do this:

ball.checkCollision(&bar1);

So, what's the difference?? It's better a way instead other?

Thank you.

解决方案

In both cases you are actually passing the address of bar1 (and you're not copying the value), since both pointers (Sprite *) and references (Sprite &) have reference semantics, in the first case explicit (you have to explicitly dereference the pointer to manipulate the pointed object, and you have to explicitly pass the address of the object to a pointer parameter), in the second case implicit (when you manipulate a reference it's as if you're manipulating the object itself, so they have value syntax, and the caller's code doesn't explicitly pass a pointer using the & operator).

So, the big difference between pointers and references is on what you can do on the pointer/reference variable: pointer variables themselves can be modified, so they may be changed to point to something else, can be NULLed, incremented, decremented, etc, so there's a strong separation between activities on the pointer (that you access directly with the variable name) and on the object that it points to (that you access with the * operator - or, if you want to access to the members, with the -> shortcut).

References, instead, aim to be just an alias to the object they point to, and do not allow changes to the reference itself: you initialize them with the object they refer to, and then they act as if they were such object for their whole life.

In general, in C++ references are preferred over pointers, for the motivations I said and for some other that you can find in the appropriate section of C++ FAQ.

In terms of performance, they should be the same, because a reference is actually a pointer in disguise; still, there may be some corner case in which the compiler may optimize more when the code uses a reference instead of a pointer, because references are guaranteed not to change the address they hide (i.e., from the beginning to the end of their life they always point to the same object), so in some strange case you may gain something in performance using references, but, again, the point of using references is about good programming style and readability, not performance.

这篇关于操作员&和*在类中的函数原型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆