Chapter11 -...

Info icon This preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
Viewing Hints  ] [  Exercise Solutions  ] [  Volume 2  ] [  Free Newsletter  ]  Seminars  ] [  Seminars on CD ROM  ] [  Consulting  ]  Thinking in C++, 2nd ed. Volume 1 © 2000 by Bruce Eckel Previous Chapter  ] [  Table of Contents  ] [  Index  ] [  Next Chapter  ]  11: References & the Copy-Constructor References are like constant pointers that are automatically dereferenced by the  compiler.  Although references also exist in Pascal, the C++ version was taken from the Algol language. They  are essential in C++ to support the syntax of operator overloading (see Chapter 12), but they are  also a general convenience to control the way arguments are passed into and out of functions. This chapter will first look briefly at the differences between pointers in C and C++, then introduce  references. But the bulk of the chapter will delve into a rather confusing issue for the new C++  programmer: the copy-constructor, a special constructor (requiring references) that makes a new  object from an existing object of the same type. The copy-constructor is used by the compiler to pass  and return objects  by value  into and out of functions. Finally, the somewhat obscure C++  pointer-to-member  feature is illuminated. Pointers in C++ The most important difference between pointers in C and those in C++ is that C++ is a more  strongly typed language. This stands out where  void*  is concerned. C doesn’t let you casually assign  a pointer of one type to another, but it  does  allow you to accomplish this through a  void* . Thus, bird* b; rock* r; void * v; v = r; b = v; Because this “feature” of C allows you to quietly treat any type like any other type, it leaves a big  hole in the type system. C++ doesn’t allow this; the compiler gives you an error message, and if 
Image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
you really want to treat one type as another, you must make it explicit, both to the compiler and to  the reader, using a cast. (Chapter 3 introduced C++’s improved “explicit” casting syntax.) References in C++ reference  ( & ) is like a constant pointer that is automatically dereferenced. It is usually used for  function argument lists and function return values. But you can also make a free-standing reference.  For example, //: C11:FreeStandingReferences.cpp #include <iostream> using namespace std; // Ordinary free-standing reference: int y; int & r = y; // When a reference is created, it must // be initialized to a live object.
Image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern