Reference vs. pointer

前端 未结 6 519
终归单人心
终归单人心 2020-12-28 08:33

What is the difference? Because this:

int Value = 50;
int *pValue = &Value;

*pValue = 88;

and ref version do the same:



        
6条回答
  •  执念已碎
    2020-12-28 09:09

    I agree with justin's answer and would like to clarify it with the tiniest example.

    Suppose you don't quite remember the syntax of a 2D image geometric library: is it

    bool BooleanOr( const Bitmap & input1, const Bitmap & input2, Bitmap * output );
    

    or is it

    bool BooleanOr( Bitmap * output, const Bitmap & input1, const Bitmap & input2 );
    

    If in your company everybody uses pointers for outputs and const references for inputs it's virtually impossible to make a mistake: when you see calls such as

    BooleanOr( thisBitmap, thatBitmap, & anotherBitmap );

    you immediately know the syntax.

提交回复
热议问题