Online Book Reader

Home Category

Professional C__ - Marc Gregoire [133]

By Root 1334 0
that takes some time to get used to. After you have worked with the examples of this chapter and experimented on your own, we hope that inheritance will become your tool of choice for object-oriented design.

Chapter 9

Understanding C++ Quirks and Oddities


WHAT’S IN THIS CHAPTER?

What the different use-cases are for references

Keyword confusion

How to use typedefs and type aliases

What scope resolution is

Details of new C++11 features that do not fit elsewhere in this book

Many parts of the C++ language have tricky syntax or quirky semantics. As a C++ programmer, you grow accustomed to most of this idiosyncratic behavior; it starts to feel natural. However, some aspects of C++ are a source of perennial confusion. Either books never explain them thoroughly enough, or you forget how they work and continually look them up, or both. This chapter addresses this gap by providing clear explanations for some of C++’s most niggling quirks and oddities.

Many language idiosyncrasies are covered in various chapters throughout this book. This chapter tries not to repeat those topics, by limiting itself to subjects that are not covered in detail elsewhere in the book. There is a bit of redundancy with other chapters, but the material is “sliced” in a different way in order to provide you with a new perspective.

The topics of this chapter include references, const, constexpr, static, extern, typedefs, type aliases, casts, scope resolution, uniform initialization, alternative function syntax, null pointer constant, angle brackets, initializer lists, explicit conversion operators, attributes, user-defined literals, header files, variable-length argument lists, and preprocessor macros. Although this list might appear to be a hodgepodge of topics, it is a carefully selected collection of some of the most confusing, but commonly used, aspects of the language, and of new C++11 features.

REFERENCES


Professional C++ code, including much of the code in this book, uses references extensively. It is helpful to step back and think about what exactly references are, and how they behave.

A reference in C++ is an alias for another variable. All modifications to the reference change the value of the variable to which it refers. You can think of references as implicit pointers that save you the trouble of taking the address of variables and dereferencing the pointer. Alternatively, you can think of references as just another name for the original variable. You can create stand-alone reference variables, use reference data members in classes, accept references as parameters to functions and methods and return references from functions and methods.

Reference Variables

Reference variables must be initialized as soon as they are created, like this:

int x = 3;

int& xRef = x;

Code snippet from References\ReferenceVariables.cpp

Subsequent to this assignment, xRef is another name for x. Any use of xRef uses the current value of x. Any assignment to xRef changes the value of x. For example, the following code sets x to 10 through xRef:

xRef = 10;

Code snippet from References\ReferenceVariables.cpp

You cannot declare a reference variable outside of a class without initializing it:

int& emptyRef; // DOES NOT COMPILE!

Code snippet from References\ReferenceVariables.cpp

You must always initialize a reference when it is created. Usually, references are created when they are declared, but reference data members need to be initialized in the constructor initializer for the containing class.

You cannot create a reference to an unnamed value such as an integer literal, unless the reference is to a const value. In the following example, unnamedRef1 will not compile because it is a non-const reference to a constant. That would mean you could change the value of the constant, 5, which doesn’t make sense. unnamedRef2 works because it’s a const reference, so you cannot write “unnamedRef2 = 7”.

int& unnamedRef1 = 5; // DOES NOT COMPILE

const int& unnamedRef2 = 5; // Works

Code snippet from References\ReferenceVariables.cpp

Return Main Page Previous Page Next Page

®Online Book Reader