`
sakakokiya
  • 浏览: 507303 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

Different between C++ pointers and references?

阅读更多
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.
分享到:
评论

相关推荐

    EC.zip_eC_inheritance

    Between templates and generic pointers? Between public and private inheritance? Between private inheritance and layering? Between function overloading and parameter defaulting? Between virtual and ...

    Understanding and Using C Pointers 原版pdf by Reese

    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(2013.5)].Richard.Reese.文字版.pdf

    本书《Understanding and Using C++ Pointers》由 Richard Reese 编写,旨在帮助读者全面了解 C++ 指针的概念及其在实际编程中的应用。通过本书的学习,读者将能够掌握指针的基础知识,理解如何声明和使用指针,以及如何...

    Chapter 4- Arrays Pointers and String.rar_C++_filmhfx

    本章“Chapter 4- Arrays Pointers and String”将深入探讨这些主题,尤其对于电影制作(filmhfx)领域的编程工作更是至关重要。 首先,数组是C++中的基本数据结构之一,它允许我们存储同类型的多个元素。数组定义...

    Programming in C++ for Engineering and Science

    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 ...

    c++官方网站标准参考资料

    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...

    Selected.Topics.in.Cplusplus.15117

    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++ ...

    C++14 Quick Syntax Reference 第2版

    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 ...

    Effective Modern C++ 原版pdf by Meyers

    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 ...

    C++ 14 Quick Syntax Reference(2nd).pdf 2016第2版

    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 BOOST C++ LIBRARIES

    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++.4th.Edition

    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 ...

    Mastering the C++17 .pdf

    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.

    Practical C++ Programming C++编程实践

    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...

    Data Structures and Algorithms in C++ 第四版

    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.A.Tutorial.and.Reference.2nd.Edition

    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–...

Global site tag (gtag.js) - Google Analytics