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 ...
c++ 11 智能指针详细解释。 清晰版本,非扫描。 c++ 11入门必备
Understanding.and.Using.C++Pointers(2013.5)].Richard.Reese.文字版.pdf
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 ...
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...
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 ...
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++ ...
Distinguish between pointers and references. 条款2:最好使用 C++ 转型操作符 Prefer C++-style casts. 条款3:绝对不要以多态(polymorphically)方式处理数组 Never treat arrays polymorphically. ...
As a professional C++ developer and former Harvard teaching fellow, I know what you need to know to be a great C++ programmer, and I know how to teach it, one step at a time. I know where people ...
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 ...
The author also endeavors to organize the C code for those who are going on to study C++ and the accompanying object-oriented approach to programming. In addition, he emphasizes how to write C code so...
If you’re an experienced C++ programmer and are anything like me, you initially approached C++11 thinking, “Yes, yes, I get it. It’s C++, only more so.” But as you learned more, you were surprised...
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 ...
Improve your programming through a solid understanding of C pointers and memory management. With this practical book, you’ll learn how pointers provide the mechanism to dynamically manipulate memory,...
This is the first book to contain content written with the C++14 standard., Tackle 42 separate C++ problems and solutions, Learn critical techniques for success on topics from smart pointers to ...
Every day, more and more people learn and use the C++ programming Language. I have taught C to thousands of students in my life. I see many of those students now moving to C++ in their school work or ...
Over the years, C++ has evolved and is used to develop software for many different sectors. Given its versatility and robustness, C++is a wonderful language to start your coding journey with. This ...
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.