Declarations and Definitions

Any identifier must be declared or defined before it is used.

Declaring a name means telling the compiler what type to associate with that name. The location of the declaration determines its scope. Scope is a region of code where an identifier can be used.

Defining an object, or variable, means allocating space and (optionally) assigning an initial value. For example,

double x, y, z;
char* p;
int i = 0;
QString message("Hello");

Defining a function means completely describing its behavior in a block of C++ statements. For example,

int max(int a, int b) {
 return a > b ? a : b;

Defining a class means specifying its structure in a sequence of declarations of function and data members.

Example 20.1. src/early-examples/decldef/point.h

class Point { <-- 1
 Point(int x, int y, int z); <-- 2
 int distance(Point other); <-- 3
 double norm() const { <-- 4
 return distance(Point(0,0,0));
 int m_Xcoord, m_Ycoord, m_Zcoord; <-- 5

(1)a class definition

(2)a constructor declaration

(3)a function declaration

(4)declaration and definition

(5)data member declaration

Example 20.2 contains some declarations that are not definitions.

Example 20.2. src/early-examples/decldef/point.cpp

extern int step; <-- 1
class Map; <-- 2
int max(int a, int b); <-- 3

(1)an object (variable) declaration

(2)a class declaration

(3)a global (non-member) function declaration

In each case, there is an implicit promise to the compiler (which will be enforced by the linker) that the declared name will be defined somewhere else in the program.

Each definition is a declaration. There can be only one definition of any name in any scope, but there can be multiple declarations.

Variable initialization is optional in C++. Nevertheless, it is strongly recommended that an initial value be provided for all variable definitions, otherwise invalid results or strange runtime errors can occur that are often difficult to locate. It is worth repeating this rule: All objects and variables should be properly initialized at creation time.

Identifier Scope

Part I: Introduction to C++ and Qt 4

C++ Introduction


Introduction to Qt



Inheritance and Polymorphism

Part II: Higher-Level Programming


Introduction to Design Patterns


Generics and Containers

Qt GUI Widgets


Validation and Regular Expressions

Parsing XML

Meta Objects, Properties, and Reflective Programming

More Design Patterns

Models and Views

Qt SQL Classes

Part III: C++ Language Reference

Types and Expressions

Scope and Storage Class

Statements and Control Structures

Memory Access

Chapter Summary

Inheritance in Detail

Miscellaneous Topics

Part IV: Programming Assignments

MP3 Jukebox Assignments

Part V: Appendices

MP3 Jukebox Assignments


MP3 Jukebox Assignments

show all menu

An Introduction to Design Patterns in C++ with Qt 4
An Introduction to Design Patterns in C++ with Qt 4
ISBN: 0131879057
EAN: 2147483647
Year: 2004
Pages: 268
Similar book on Amazon © 2008-2017.
If you may any questions please contact us: