0
MyObject obj = new MyObject(k);
foo(obj);
bar(&obj);

foo(MyObject &)
bar(MyObject *)

I know we need to use the passed parameter differently in the two functions, but apart from that, in terms of memory allocation or otherwise, is there a difference between the two? I mean for the reference also the compiler would be storing the memory pointer.

4

2 に答える 2

1

コンパイラ (つまり、生成されたマシン コード) の観点からは、実質的な違いはありません。

確かではありませんが、 cfrontが参照引数のポインターへの直接変換を使用していることを発見しても驚かないでしょう。

ただし、言語レベルでは多くの違いがあります。たとえば、参照は正式には常にオブジェクトにバインドされ (つまり、「NULL 参照」はありません)、参照を再バインドすることもできません (ポインター変数は別のものを指すようにすることができます)。

于 2013-09-29T19:40:34.747 に答える