Skip to main content

Professional C++, 2nd Edition

Professional C++, 2nd Edition

Marc Gregoire, Nicholas A. Solter, Scott J. Kleper

ISBN: 978-0-470-93244-5

Oct 2011

1104 pages

Description

Essential reading for experienced developers who are determined to master the latest release of C++

Although C++ is often the language of choice from game programming to major commercial software applications, it is also one of the most difficult to master. With this no-nonsense book, you will learn to conquer the latest release of C++. The author deciphers little-known features of C++, shares detailed code examples that you can then plug into your own code, and reveals the significant changes to C++ that accompany the latest release. You'll discover how to design and build applications that solve real-world problems and then implement the solution using the full capabilities of the language.

Appeals to experienced developers who are looking for a higher level of learning

  • Drills down the extensive changes to the latest C++ standard, C++11, including enhancements made to run-time performance, standard library, language usability, and core language
  • Zeroes in on explaining the more poorly understood elements of the C++ feature set and addresses common pitfalls to avoid
  • Includes case studies that feature extensive, working code that has been tested on Windows and Linux platforms
  • Intertwines text with useful tips, tricks, and workarounds

Packed with best practices for programming, testing, and debugging applications, this book is vital for taking your C++ skills to the next level.

Related Resources

Instructor

Request an Evaluation Copy for this title

INTRODUCTION xxv

PART I: INTRODUCTION TO PROFESSIONAL C++

CHAPTER 1: A CRASH COURSE IN C++ 3

CHAPTER 2: DESIGNING PROFESSIONAL C++ PROGRAMS 49

CHAPTER 3: DESIGNING WITH OBJECTS 77

CHAPTER 4: DESIGNING FOR REUSE 97

CHAPTER 5: CODING WITH STYLE 111

PART II: C++ CODING THE PROFESSIONAL WAY

CHAPTER 6: GAINING PROFICIENCY WITH CLASSES AND OBJECTS 133

CHAPTER 7: MASTERING CLASSES AND OBJECTS 169

CHAPTER 8: DISCOVERING INHERITANCE TECHNIQUES 211

CHAPTER 9: UNDERSTANDING C++ QUIRKS AND ODDITIES 267

CHAPTER 10: HANDLING ERRORS 317

CHAPTER 11: DELVING INTO THE STANDARD LIBRARY 361

CHAPTER 12: UNDERSTANDING CONTAINERS AND ITERATORS 385

CHAPTER 13: MASTERING STL ALGORITHMS 457

CHAPTER 14: USING STRINGS AND REGULAR EXPRESSIONS 509

CHAPTER 15: DEMYSTIFYING C++ I/O 545

CHAPTER 16: ADDITIONAL LIBRARY UTILITIES 569

CHAPTER 17: CUSTOMIZING AND EXTENDING THE STL 595

PART III: MASTERING ADVANCED FEATURES OF C++

CHAPTER 18: OVERLOADING C++ OPERATORS 645

CHAPTER 19: WRITING GENERIC CODE WITH TEMPLATES 683

CHAPTER 20: ADVANCED TEMPLATES 715

CHAPTER 21: EFFECTIVE MEMORY MANAGEMENT 759

CHAPTER 22: MULTITHREADED PROGRAMMING WITH C++ 797

PART IV: C++ SOFTWARE ENGINEERING

CHAPTER 23: MAXIMIZING SOFTWARE ENGINEERING METHODS 839

CHAPTER 24: WRITING EFFICIENT C++ 859

CHAPTER 25: DEVELOPING CROSS-PLATFORM AND CROSS-LANGUAGE APPLICATIONS 883

CHAPTER 26: BECOMING ADEPT AT TESTING 905

CHAPTER 27: CONQUERING DEBUGGING 927

CHAPTER 28: INCORPORATING DESIGN TECHNIQUES AND FRAMEWORKS 967

CHAPTER 29: APPLYING DESIGN PATTERNS 985

APPENDIX A: C++ INTERVIEWS 1013

APPENDIX B: ANNOTATED BIBLIOGRAPHY 1035

APPENDIX C: STANDARD LIBRARY HEADER FILES 1045

INDEX 1053

Bonus Chapter 1
Download
Bonus Chapter 2
Download
ReadMe File
Download
Full Code Download
Updated 10/28/11 wit new std_array.cpp file for Chapter 1. Gameboard (in Chapter 19) was updated on 8/2/12.
Download
Chapter 1
std_array.cpp updated 10/28/11
Download
std_array.cpp
This CPP file replaces the one in the code archive for Chapter 1 (932445c01_code/std_array/std_array.cpp). It contains the below fix for page 22: #include #include using namespace std; int main() { array arr = {9, 8, 7}; cout << "Array size = " << arr.size() << endl; for (auto i : arr) cout << i << endl; return 0; }
Download
Chapter 6
Download
Chapter 7
Download
Chapter 8
Download
Chapter 9
Download
Chapter 10
Download
Chapter 11
Download
Chapter 12
Download
Chapter 13
Download
Chapter 14
Download
Chapter 15
Download
Chapter 16
Download
Chapter 17
Download
Chapter 18
Download
Chapter 19
The Gameboard file was updated on 8/2/12.
Download
Chapter 20
Download
Chapter 21
Download
Chapter 22
Download
Chapter 24
Download
Chapter 25
Download
Chapter 26
Download
Chapter 27
Download
Chapter 28
Download
Chapter 29
Download
Gameboard (for Chapter 19)
Updated Gameboard code on 8/2/12.
Download
ChapterPageDetailsDatePrint Run
22Error in Code
Reads:
#include <iostream>
#include <array>
using namespace std;
int main()
{
                array<int, 3> arr = {9, 8, 7};
                cout << "Array size = " << arr.size() << endl;
                for (auto i : arr)
                                cout << i << endl;
                                cout << *iter << endl;

                return 0;
}

Should be:
#include <iostream>
#include <array>
using namespace std;
int main()
{
                array<int, 3> arr = {9, 8, 7};
                cout << "Array size = " << arr.size() << endl;
                for (auto i : arr)
                                cout << i << endl;

                return 0;
}


10/26/2011

28Error in Code
Currently reads:
char* pointerString = "Hello, World";

Should read:
const char* pointerString = "Hello, World";
06/04/2012

7181Error in Code
Currently Reads:
static int sCounter = 0;
Should Read:
static int sCounter;

Followed by the following paragraph:
Currently Reads:
In addition to listing static class members in the class defi nition, you will have to allocate space for them in a source fi le, usually the source fi le in which you place your class method defi nitions.You can initialize them at the same time, but note that unlike normal variables and data members, they are initialized to 0 by default. static pointers are initialized to nullptr. Here is the code to allocate space for and initialize the sCounter member in pre-C++11:

Should Read:
In addition to listing static class members in the class definition, you will have to allocate space for them in a source file, usually the source file in which you place your class method definitions. You can initialize them at the same time, but note that unlike normal variables and data members, they are initialized to 0 by default. static pointers are initialized to nullptr. Here is the code to allocate space for and initialize the sCounter member:
Currently Reads:
int Spreadsheet::sCounter = 0;
Should Read:
int Spreadsheet::sCounter;

This code appears outside of any function or method bodies. It's almost like declaring a global variable, except that the Spreadsheet:: scope resolution specifies that it's part of the Spreadsheet class.
6 June 2015

232Error in Text
The sentence:
?No code needs to be written in the .cpp file, and any attempt to write code will cause a compiler error.?

Should become:
?No code needs to be written in the .cpp file.?
08/17/2012

xiii, 258Error in Text
Currently reads:
Copy Constructors and the Equals Operator in Subclasses

Should read:
Copy Constructors and the Assignment Operator in Subclasses
06/04/2012

390Error in Text
The first sentence on this page says:
"The standard containers that provide iterators all furnish either random access or bidirectional iterators."

This is untrue and should be deleted.
06/04/2012

414Error in Text
Sentence in second paragraph currently reads:
"A list supports begin(), returning an iterator referring to the first element in the list, and end(), returning an iterator referring to the last element in the list."

Sentence should read:
"A list supports begin(), returning an iterator referring to the first element in the list, and end(), returning an iterator referring to one past the last element in the list."
06/04/2012

468Error in Code
Code currently reads:
int cnt = count_if(vec.cbegin(),vec.cend(),
[=, &cntLambdaCalled](int i){++cntLambdaCalled; return i>value;});


Code should read:
int cnt = count_if(vec.cbegin(),vec.cend(),
                     [=, &cntLambdaCalled](int i)->bool{++cntLambdaCalled; return i>value;});
06/04/2012

487Error in Text
The description of lexicographical_compare() currently reads:
lexicographical_compare() deals with the situation where the two ranges may contain different numbers of elements. It returns true if all the elements in the first range are less than their corresponding elements in the second range, or, if the first range has fewer elements than the second and all elements in the first range are less than their corresponding initial subsequence in the second set.

This should read:
"... It returns true if the first unequal element in the first range is less than its corresponding element in the second range, or, if the first range has fewer elements than the second and all elements in the first range are equal to their corresponding initial subsequence in the second set."
06/04/2012

596Error in Text
Currently reads:
"This regular expression searches for spaces, newlines, form feeds, and back slashes."

Should read:
"... newlines, carriage returns, and back slashes."
06/04/2012

596Error in Text
Currently reads:
"Every reversible container in the STL, which happens to be every container that's part of the standard, ..."

Should read:
"Every reversible container in the STL, which happens to be every container that's part of the standard, ...except forward_list and the unordered associative containers".
06/04/2012

685Error in Code
Please replace current code Gameboard.H with:
class GameBoard
{
public:
       // The general-purpose GameBoard allows the user to specify its dimensions
       GameBoard(size_t inWidth = kDefaultWidth, size_t inHeight = kDefaultHeight);
       GameBoard(const GameBoard& src); // copy constructor
       virtual ~GameBoard();
       GameBoard& operator=(const GameBoard& rhs); // assignment operator

       void setPieceAt(size_t x, size_t y, const GamePiece* inPiece);
       GamePiece* getPieceAt(size_t x, size_t y);
       const GamePiece* getPieceAt(size_t x, size_t y) const;

       size_t getHeight() const { return mHeight; }
       size_t getWidth() const { return mWidth; }
       static const size_t kDefaultWidth = 10;
       static const size_t kDefaultHeight = 10;

protected:
       void copyFrom(const GameBoard& src);
       void initializeCells();
       void cleanupCells();
       // objects dynamically allocate space for the game pieces.
       GamePiece*** mCells;
       size_t mWidth, mHeight;
};
06/04/2012

685Error in Text
The last paragraph currently reading:
...?getPieceAt() returns a reference to the piece?? and ends with ??before using the returned reference.?

This should be deleted.
06/04/2012

686Error in Text
The note at the top of the page currently reads:
"This implementation of the class provides two versions of getPieceAt(), one of which returns a reference and one of which returns a const reference."

This should should be deleted.
06/04/2012

686-687Error in Code
The code on pages 686-687 (Gameboard.cpp) should be deleted and replaced with the following:

GameBoard::GameBoard(size_t inWidth, size_t inHeight) :
  mWidth(inWidth), mHeight(inHeight)
{
  initializeCells();
}
GameBoard::GameBoard(const GameBoard& src)
{
  copyFrom(src);
}
GameBoard::~GameBoard()
{
  // free the old memory
  cleanupCells();
}
void GameBoard::copyFrom(const GameBoard& src)
{
  mWidth = src.mWidth;
  mHeight = src.mHeight;

  initializeCells();

  for (size_t i = 0; i < mWidth; i++) {
    for (size_t j = 0; j < mHeight; j++) {
      if (src.mCells[i][j])
        mCells[i][j] = src.mCells[i][j]->Clone();
    }
  }
}
void GameBoard::initializeCells()
{
  mCells = new GamePiece** [mWidth];
  for (size_t i = 0; i < mWidth; i++) {
    mCells[i] = new GamePiece*[mHeight];
    for (size_t j = 0; j < mHeight; j++) {
      mCells[i][j] = nullptr;
    }
  }
}
void GameBoard::cleanupCells()
{
  for (size_t i = 0; i < mWidth; i++) {
    for (size_t j = 0; j < mHeight; j++) {
      delete mCells[i][j];
    }
    delete [] mCells[i];
  }
  delete [] mCells;
  mCells = nullptr;
}
GameBoard& GameBoard::operator=(const GameBoard& rhs)
{
  // check for self-assignment
  if (this == &rhs) {
    return *this;
  }
  // free the old memory
  cleanupCells();
  // copy the new memory
  copyFrom(rhs);
  return *this;
}
void GameBoard::setPieceAt(size_t x, size_t y, const GamePiece* inElem)
{
  if (inElem)
    mCells[x][y] = inElem->Clone();
}
GamePiece* GameBoard::getPieceAt(size_t x, size_t y)
{
  return mCells[x][y];
}
const GamePiece* GameBoard::getPieceAt(size_t x, size_t y) const
{
  return mCells[x][y];
}



06/04/2012

687Error in Code
Currently reads:
GameBoard chessBoard(8, 8);
ChessPiece pawn;
chessBoard.setPieceAt(0, 0, pawn);


Should read:
GameBoard chessBoard(8, 8);
ChessPiece pawn;
chessBoard.setPieceAt(0, 0, &pawn);

06/04/2012

689Error in Text
Text currently reads:
?mCells is now a T** instead of a GamePiece** because??

Should read:
?mCells is now a T** instead of a GamePiece*** because??
06/04/2012

803-804Error in Text
Currently reads: 10.000 and 100.000

Should read: 10,000 and 100,000.
06/10/2013

1053Error in text
Currently reads:
- - decrement operator, 11

Should read:
-- decrement operator, 11 (space between symbols)
04/25/2012

22Text Correction: Error in Code
there is no code example for pool threads in Chapter 22 of Professional C++, 2nd Edition. Is there intended or is code meant to be available?
05/01/15