MFC Programmer's SourceBook : Thinking in C++
Bruce Eckel's Thinking in C++, 2nd Ed Contents | Prev | Next

Exercises

  1. Create a function that takes a char& argument and modifies that argument. In main( ), print out a char variable, call your function for that variable, and print it out again to prove to yourself it has been changed. How does this affect program readability?
  2. Write a class with a copy-constructor that announces itself to cout. Now create a function that passes an object of your new class in by value and another one that creates a local object of your new class and returns it by value. Call these functions to prove to yourself that the copy-constructor is indeed quietly called when passing and returning objects by value.
  3. Discover how to get your compiler to generate assembly language, and produce assembly for PassStruct.cpp. Trace through and demystify the way your compiler generates code to pass and return large structures.
  4. (Advanced) This exercise creates an alternative to using the copy-constructor. Create a class X and declare (but don’t define) a private copy-constructor. Make a public clone( ) function as a const member function that returns a copy of the object created using new (a forward reference to Chapter 11). Now create a function that takes as an argument a const X& and clones a local copy that can be modified. The drawback to this approach is that you are responsible for explicitly destroying the cloned object (using delete) when you’re done with it.
Contents | Prev | Next


Go to CodeGuru.com
Contact: webmaster@codeguru.com
© Copyright 1997-1999 CodeGuru