`
sakakokiya
  • 浏览: 490094 次
  • 性别: 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 ...

    Using C++11 Smart Pointers

    c++ 11 智能指针详细解释。 清晰版本,非扫描。 c++ 11入门必备

    Understanding.and.Using.C++Pointers(2013.5)].Richard.Reese.文字版.pdf

    Understanding.and.Using.C++Pointers(2013.5)].Richard.Reese.文字版.pdf

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

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

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

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

    More_Effective_C++中文.pdf

     Distinguish between pointers and references.  条款2:最好使用 C++ 转型操作符  Prefer C++-style casts.  条款3:绝对不要以多态(polymorphically)方式处理数组  Never treat arrays polymorphically. ...

    Jumping Into C++ 完整英文版.pdf

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

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

    Kenneth Reek - Pointers on C

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

    高效现代C++(Effective Modern C++)(高清英文原版)

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

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

    Eng - Understanding and Using C Pointers

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

    effective modern c++

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

    C++ by Example

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

    C++17 By Example

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

Global site tag (gtag.js) - Google Analytics