Online Book Reader

Home Category

Professional C__ - Marc Gregoire [473]

By Root 1486 0
you’re coding, but plan for bugs in your code. The reality of programming is that bugs will appear. If you’ve prepared your program properly, with error logging, debug traces, asserts, and static asserts, then the actual debugging will be significantly easier.

In addition to these techniques, this chapter also presented specific approaches for debugging bugs. The most important rule when actually debugging is to reproduce the problem. Then, you can use message-based debugging or a symbolic debugger to track down the root cause. Memory errors present particular difficulties, and account for the majority of bugs in C++ code. This chapter described the various categories of memory bugs and their symptoms, and showed examples of debugging errors in a program.

Chapter 28

Incorporating Design Techniques and Frameworks


WHAT’S IN THIS CHAPTER?

An overview of C++ language features that are common but involve easy-to-forget syntax

What the double dispatch technique is and how to use it

How to use mix-in classes

What frameworks are

One of the major themes of this book has been the adoption of reusable techniques and patterns. As a programmer, you tend to face similar problems repeatedly. With an arsenal of diverse approaches, you can save yourself time by applying the proper technique to a given problem.

A design technique is a standard approach for solving a particular problem in C++. Often, a design technique aims to overcome an annoying feature or language deficiency. Other times, a design technique is a piece of code that you use in many different programs to solve a common C++ problem.

This chapter focuses on design techniques — C++ idioms that aren’t necessarily built-in parts of the language, but are nonetheless frequently used. The first part of this chapter covers the language features in C++ that are common but involve easy-to-forget syntax. Most of this material is a review, but it is a useful reference tool when the syntax escapes you. The topics covered include:

Starting a class from scratch

Extending a class with a subclass

Throwing and catching exceptions

Reading from a file

Writing to a file

Defining a template class

The second part of this chapter focuses on higher-level techniques that build upon C++ language features. These techniques offer a better way to accomplish everyday programming tasks. Topics include:

The double dispatch technique

Mix-in classes

This chapter concludes with an introduction to frameworks, a coding technique that greatly eases the development of large applications.

“I CAN NEVER REMEMBER HOW TO ...”


Chapter 1 compares the size of the C standard to the size of the C++ standard. It is possible, and somewhat common, for a C programmer to memorize the entire C language. The keywords are few, the language features are minimal, and the behaviors are well defined. This is not the case with C++. Even the authors of this book need to look things up. With that in mind, this section presents examples of coding techniques that are used in almost all C++ programs. When you remember the concept but forget the syntax, turn to these pages for a refresher.

... Write a Class

Don’t remember how to get started? No problem — here is the definition of a simple class:

#ifndef _simple_h_

#define _simple_h_

// A simple class that illustrates class definition syntax.

class Simple

{

public:

Simple(); // Constructor

virtual ~Simple(); // Destructor

virtual void publicMethod(); // Public method

int mPublicInteger; // Public data member

protected:

int mProtectedInteger; // Protected data member

static const int mConstant = 2; // Protected constant

static int sStaticInt; // Protected static data member

private:

int mPrivateInteger; // Private data member

// Disallow assignment and pass-by-value

Simple(const Simple& src);

Simple& operator=(const Simple& rhs);

};

#endif

Code snippet from Simple\Simple.h

Note that this class definition shows what is possible. However, in your own class definitions, you should try to avoid having public data members. Instead,

Return Main Page Previous Page Next Page

®Online Book Reader