开发者

Why cant we use reference in algorithms and data structure?

As a general rule, use references in function parameters and return types to define attractive interfaces. Use pointers to implement algorithms and data structures.

I read thi开发者_如何学运维s line in an article.I had the doubt of why reference cannot be used in algorithms and datastructures?

Please help me undestand why is it.


Because a reference can't be made to refer to any other object after it's been initialised to refer to a particular one.

As proof, §8.5.3.2 of the Standard says

A reference cannot be changed to refer to another object after initialization. Note that initialization of a reference is treated very differently from assignment to it. Argument passing (5.2.2) and function value return (6.6.3) are initializations.

Data structures and algorithms (though to a lesser extent) usually involve adding, removing, and reordering objects within themselves. With references you cannot do this, so you use pointers to do it inexpensively.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜