Because a reference must refer to an object, C++ requires that references be initialized.
Notice that there is no such thing as a null reference.
Another important difference between pointers and references is that pointers may be reassigned to refer to different objects. A reference, however, always refers to the object with which it is initialized
In general, you should use a pointer whenever you need to take into account the possibility that there's nothing to refer to (in which case you can set the pointer to null) or whenever you need to be able to refer to different things at different times (in which case you can change where the pointer points). You should use a reference whenever you know there will always be an object to refer to and you also know that once you're referring to that object, you'll never want to refer to anything else
Other occassions: you'll almost always want operator[] to return a reference.
分享到:
相关推荐
Between templates and generic pointers? Between public and private inheritance? Between private inheritance and layering? Between function overloading and parameter defaulting? Between virtual and ...
Rarely do they venture beyond a basic treatment of pointers and most give only cursory coverage of the important memory management technology involving the stack and the heap. Yet without this ...
本书《Understanding and Using C++ Pointers》由 Richard Reese 编写,旨在帮助读者全面了解 C++ 指针的概念及其在实际编程中的应用。通过本书的学习,读者将能够掌握指针的基础知识,理解如何声明和使用指针,以及如何...
本章“Chapter 4- Arrays Pointers and String”将深入探讨这些主题,尤其对于电影制作(filmhfx)领域的编程工作更是至关重要。 首先,数组是C++中的基本数据结构之一,它允许我们存储同类型的多个元素。数组定义...
Chapter 13: Multidimensional Arrays and Vectors Chapter 14: Building ClassesChapter 15: Pointers and Linked Structures Chapter 16: Data Structures Answers to Test Yourself Questions Appendix A: ASCII ...
Distinguish between pointers and references in C++, Comparison of popular compilers and IDEs, Book Of Brilliant Things, Copy constructors, assignment operators, and exception safe assignment, How...
It has pointers and references. You need to know how and when you should use them. Then you have virtual functions and virtual tables and virtual pointers. When you know these core concepts C++ ...
What are C++ Variables, Operators, Pointers and References What are Arrays, Strings, Conditionals, Loops and more How to use Functions How to work with Constructors and Inheritance How to use Access ...
range-based for loops, lambda expressions, and rvalue references change the face of C++, to say nothing of the new concurrency features. And then there are the idiomatic changes. 0 and typedefs are ...
What are C++ Variables, Operators, Pointers and References What are Arrays, Strings, Conditionals, Loops and more How to use Functions How to work with Constructors and Inheritance How to use Access ...
11.3 Pointers and references 11.4 Serialization of class hierarchy objects 11.5 Wrapper functions for optimization 11.6 Exercises Chapter 12: Parser 12.1 General 12.2 Extended Backus-Naur Form 12.3 ...
Data Structures and Other Objects Using C++ takes a gentle approach to the data structures course in C++. Providing an early, self-contained review of object-oriented programming and C++, this text ...
The C++ language has a long history, dating back to the 1980s. Recently it has undergone a renaissance, ...class and function templates, the difference between lvalue and rvalue references, and so on.
Simple Pointers const Pointers Pointers and Printing Pointers and Arrays The reinterpret_cast Pointers and Structures Command-Line Arguments Programming Exercises Answers to Chapter Questions Part IV...
Chapter 1 presents the basic principles of object-oriented programming, an introductionto dynamic memory allocation and the use of pointers, and a rudimentarypresentation of the Standard Template ...
The C++ standard library provides a set of common classes and interfaces that greatly extend the core C++ language. The library, however, is not self-explanatory. To make full use of its components–...