Chapter11 -...

Info iconThis 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  ]  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 
Background image of page 1

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

View Full DocumentRight 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. // However, you can also say:
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 08/10/2011 for the course IT 331 taught by Professor Nevermind during the Spring '11 term at King Abdulaziz University.

Page1 / 25

Chapter11 -...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online