0-reference.cc --------------- C++ supports real references. A reference is an alias - another name for something. A reference must be initialized when it is created. There is no way to change what the reference is aliasing. For example, int &z = x; z = y; Here if you say 'z' you're talking about setting the value of x to y. There's no way to say, "No, I want z to now be an alias for y." 1-callbyreference.cc -------------------- A natural use of C++ references is to pass arguments by reference. That allows the called method to change the value of the caller's passed variables without having to use pointers. When do you use references and when pointers? It's a matter of convention. - Use references if the called method intends to read and possibly modify the caller's variable - Use pointers for output parameters - the called method is returning more than one thing, say, and so one or more parameters are write-only - Use const ref for read-only parameters that are large and so you don't want to pass by value 2-brokencallbyrefconst.cc ------------------------- You can combine const and references, but if you do you can't modify the thing referenced. 3-constmadness.cc ----------------- (A) const int y = 6; can be written as int const y = 6; (B) Read from the right: int const * const u means u is a constant pointer to a constant integer. (C) How is const difference from #define? 4-PointInline.h --------------- One way to reduce the cost of invoking a method is to "inline" the code for the method. That is, rather than use the procedure call linkage to invoke a single instance of the compiled code, insert the (effect of) the method's code into the caller at teh point of the call. Especially for very small methods that can be substantially faster. If the code contains many calls to the method, though, the code can be much larger. There might also be a cache perfromance penalty to inlining. Obviously, to inline the compiler must know what the inlined method's code is. In C++, you can put the code for methods in the .h file. This is a HINT to the compiler that you would like it to inline. (If you don't put the code in the .h there's no way for it to inline.) Whether or not it does is up to it. There are two different syntatic versions of inlining in the .h file. One looks exactly like Java -- define the method where (in C++) you would ordinarily just declare it. The other is declare it and then at global scope define the method as inline. Which to use depends. If the method is small, usually you define inside the class definition. If it's big, it's often better to define at global scope so that the .h can be read to easily determine what methods the class supports. 5-PointInitializer.h -------------------- Usually we initialize object instance variables in the constructor. C++ also provides initializers - a way to initialize before we reach the first executable line of the constructor. Why? See the next example. 6-PointRefInitializer.h ----------------------- If the class defines instance variables that are references, there is an issue. References must be initialized when created. By the time we get to the object's constructor, it's too late. If x is a reference, there's no statement that will change what it is x references (so there's no way to write an assignment in the constructor). In such situations you have to use initializers. 7-constCast.cc -------------- (A) A method can be declared const. A const method is one that "guarantees" it doesn't modify the instance it invoked on. (B) Except that this is C++ and so there's a way to undo the const and have the method modify the object. (Never do that!)