Tải bản đầy đủ (.pdf) (143 trang)

Advanced c++ programming in linux

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (955.56 KB, 143 trang )

Version 3.1— June 2003

Advanced C++
Programming
Jean-Paul R IGAULT

Professor, University of Nice Sophia Antipolis
École Supérieure en Sciences Informatiques (ESSI)
930 route des Colles – BP 145
06903 S OPHIA A NTIPOLIS Cedex – France
Email :

© 1996-2003 — Jean-Paul R IGAULT


Advanced C++ Programming
Foreword
This document was originally designed as handouts for a third (last) year semester course at ESSI,
the Computer Engineering School of the University of Nice Sophia Antipolis. The course was first
given in the second semester of 1996. It has since been used in continuing education sessions on
Advanced C++ in various contexts for several industrial companies.
The present version (3.1) describes a C++ language which complies with the ISO C++ standard
(ISO/IEC 14882:1998(E) [13]). Some features described here may not be supported event by
recent C++ compilers. Most of them are known to work with gcc-2.95.3, and all of them with gcc3.2.x.
The original document was in French. Only a (large) part of the course has been translated into
English.
Needless to say that the order for reading slides is the following:

1

2



3

4

© 1996-2003 — Jean-Paul R IGAULT — Version 3.1

II


Advanced C++ Programming
Slides List
Foreword

0-ii

Slides List

0-iii

Contents of the Course . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .0-2

1.Introduction
Introduction

1-1
1-2

Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .1-2


C++ Characteristics

1-3

C and the Scandinavian School . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .1-3
Origin and Influences . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .1-4
Why is C++ a Difficult Language? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .1-5

References for this Course

1-6

Indispensable Books . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .1-6

2.Fundamental Mechanisms in C++

2-1

Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .2-2

Objects and Types

2-3

Strong Typing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .2-3
Strong Typing... but Implicit Conversions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .2-4
Strong Typing Makes Overloading Possible . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .2-5
Different Sorts of Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .2-7

The C++ Class


2-8

What’s in a Class? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .2-8
The “Natural” Generalization of struct . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .2-9
A Model for Creating Instances . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-11
A Model for Creating Instances: new and delete . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-12
An (Abstract) Data Type . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-16
An (Abstract) Data Type: Redefining Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-17
A Name Space . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-18
A Name Space: Hierarchical in case of Inheritance . . . . . . . . . . . . . . . . . . . . . . . . . . 2-19
An Encapsulation Unit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-20
An Encapsulation Unit: Protected Members . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-21
A Reuse Unit through Inheritance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-22
A Reuse Unit through Inheritance: virtual functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-23
© 1996-2003 — Jean-Paul R IGAULT — V ERSION 3.1

III


Advanced C++ Programming

Slides list

A Reuse Unit through Composition . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-24

Objects Handling

2-25


Creation, Utilization, and Implementation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-25

Creating, deleting, and initializing objects
A Guarantee of Initialization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Modes for Creating/Deleting Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Constructions/Destructions Order . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Default Construction of Built-in Scalar Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

Copying Objects
Copy during Initialization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Copy during Assignment . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Initialization and Assignment . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Default Copy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Default Copy: Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Deep and Shallow Copy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Value Semantics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Preventing Copy of Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

3.Advanced Mechanisms and ANSI Extensions

2-26
2-26
2-27
2-29
2-32

2-33
2-33
2-34
2-35

2-36
2-37
2-38
2-39
2-40

3-1

Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .3-2

Exceptions

3-3

Motivation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .3-3
Basic Mechanism . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .3-4
Basic Mechanism: Destruction of Automatic Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . .3-5
Basic Mechanism: Reraising (Rethrowing) an Exception . . . . . . . . . . . . . . . . . . . . . . . . .3-6
Basic Mechanism: Uncaught Exceptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .3-7
Exceptions Thrown from within a Constructor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .3-8
Throw List of a function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-11
Organization of Exceptions: catch Resolution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-12
Organization of Exceptions: Exceptions Hierarchy . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-13
Organization of Exceptions: Class exception . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-15
Organization of Exceptions: Naming Exceptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-17
Resource Acquisition is Initialization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-18
Termination or Resume? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-20
Exceptions Should Remain Exceptional . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-21

Run Time Type Information (RTTI)

Motivation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Operator dynamic_cast . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Class type_info and Operator typeid . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Bad Use of RTTI . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The Zoology of ANSI C++ Casts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

Multiple Inheritance

3-22
3-22
3-24
3-26
3-30
3-31

3-34

Reminders . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-34
Virtual Derivation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-35
Construction of Virtual Base Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-36

© 1996-2003 — Jean-Paul R IGAULT — Version 3.1

IV


Advanced C++ Programming

Slides list


Conversions and Virtual Base Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-38

Generic Functions and Classes (template)
General Picture . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Generic Functions: Definition . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Generic Functions: Instantiation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Generic Functions: Specialization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Generic Functions: Utilization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Generic Classes: Definition . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Generic Classes: Definition of Member and Friend Functions . . . . . . . . . . . . . . . . . . .
Generic Classes: Instantiation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Generic Classes and Class Derivation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Generic Classes and Friendship . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Generic Classes: Initialization of Static Members . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Member Template and Friends . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Generic Parameters for Classes and Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Generic Classes and Implicit Conversions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Explicit Instantiation of Generic Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Generic Classes: Total Specialization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Generic Classes: Partial Specialization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The Problem of Automatic Template Instantiation . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Automatic Template Instanciation: AT&T C++/Sun CC . . . . . . . . . . . . . . . . . . . . . . . .
Automatic Template Instantiation: GNU gcc 2.9x . . . . . . . . . . . . . . . . . . . . . . . . . . . .

Miscellaneous ANSI C++ Extensions
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Keywords . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Really Miscellaneous . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Scope of Loop Indexes; Variables in Conditions . . . . . . . . . . . . . . . . . . . . . . . . . . . . .


Pointers to Class Members
Motivation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Utilization of Pointers to Member-Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Pointers to Data Members . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Standard Conversions of Pointers to Members . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

Name Packaging (namespace)
Motivation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Definition . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Utilization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Synonyms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Backwards Compatibility . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Class as a Namespace . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

References

© 1996-2003 — Jean-Paul R IGAULT — Version 3.1

3-40
3-40
3-41
3-42
3-43
3-44
3-45
3-46
3-47
3-48
3-49
3-50

3-51
3-53
3-55
3-56
3-58
3-59
3-61
3-63
3-65

3-66
3-66
3-67
3-68
3-72

3-73
3-73
3-75
3-76
3-77

3-78
3-78
3-79
3-80
3-81
3-82
3-84


B-1

V


Advanced C++ Programming

© 1996-2003 — Jean-Paul R IGAULT — Version 3.1

Slides list

VI


Advanced C++ Programming
Jean-Paul R IGAULT
Professor, University of Nice Sophia Antipolis
École Supérieure en Sciences Informatiques (ESSI)
930 route des Colles – BP 145 – 06903 S OPHIA A NTIPOLIS Cedex – France
Email :

© 1996, 2003 – Jean-Paul RIGAULT — Version 3.1

0-1


Advanced C++ Programming

Advanced C++ Programming
Contents of the Course

1. Introduction
2. Fundamental Mechanisms in C++
3. Advanced Mechanisms and ANSI Extensions

© 1996, 2003 – Jean-Paul RIGAULT — Version 3.1

0-2


Advanced C++ Programming

1. Introduction

© 1996, 2003 – Jean-Paul RIGAULT — Version 3.1

1-1


Advanced C++ Programming

1. Introduction

Introduction
Summary
❏ C++ characteristics
❏ The object-oriented approach
❏ References

© 1996, 2003 – Jean-Paul RIGAULT — Version 3.1


1-2


Advanced C++ Programming

1. Introduction

C++ Characteristics
C and the Scandinavian School
❏ Scandinavian school of object-oriented programming
❏ Hybrid language
◆ Support for procedural (imperative) style
◆ Object-oriented features
❏ Compiled language
❏ Programming safety and run-time efficiency
◆ Maximal static analysis
❏ Upward compatible with (ANSI) C
© 1996, 2003 – Jean-Paul RIGAULT — Version 3.1

1-3


Advanced C++ Programming

1. Introduction

C++ Characteristics
Origin and Influences
60


70

80

90
Ada 95

Ada

Fortran
Algol 60

Algol 68

CPL

BCPL

C

ANSI C
C with
classes

Simula 67

ML

C++


C++
ARM

C++
std

Eiffel

CLU

Objective C
Java

Smalltalk
Lisp
© 1996, 2003 – Jean-Paul RIGAULT — Version 3.1

CLOS
1-4


Advanced C++ Programming

1. Introduction

C++ Characteristics
Why is C++ a Difficult Language?
❏ Language mechanisms are intrinsically complex
◆ Need to master the fundamental concepts of
programming languages

◆ However the language is consistent and has a clear
philosophy
❏ Object-orientation is a culture
◆ Stress on architecture
◆ Need to give up centralized and purely functional reflexes
and habits
© 1996, 2003 – Jean-Paul RIGAULT — Version 3.1

1-5


Advanced C++ Programming

1. Introduction

References for this Course
Indispensable Books

(1)

[27] The C++ Programming Language – Third Edition
Bjarne STROUSTRUP – Addison-Wesley, 1997
[6]

Advanced C++
Programming Style and Idioms
James O. COPLIEN – Addison Wesley, 1992

[28] The Design and Evolution of C++
Bjarne STROUSTRUP – Addison-Wesley, 1994

[13] International Standard for Information Systems
Programming Language C++
ISO/IEC 14882:1998(E)

© 1996, 2003 – Jean-Paul RIGAULT — Version 3.1

1-6


Advanced C++ Programming

1. Introduction

References for this Course
Indispensable Books

(2)

[14] The C++ Standard Template Library
Nicolai M. JOSUTIS – Addison-Wesley, 1999
[25] Effective C++ CD:
85 Specific Ways to Improve your Programs and Design
Scott MEYERS – Addison-Wesley, 2000
[1]

Modern C++ Design:
Generic Programming and Design Patterns Applied
Andrei ALEXANDESCU – Addison-Wesley, 2001

[10] Design Patterns

Elements of Reusable Object-Oriented Software
Erich GAMMA et al. – Addison-Wesley, 1995
(also available as a CD )
© 1996, 2003 – Jean-Paul RIGAULT — Version 3.1

1-7


Advanced C++ Programming

2. Fundamental Mechanisms in
C++

© 1996, 2003 – Jean-Paul RIGAULT — Version 3.1

2-1


Advanced C++ Programming

2. Fundamental Mechanisms in C++

Fundamental Mechanisms in C++
Summary
❏ Objects and Types
❏ The C++ class
❏ Objects handling
❏ Creating, deleting, and initializing objects
❏ Copying objects


© 1996, 2003 – Jean-Paul RIGAULT — Version 3.1

2-2


Advanced C++ Programming

2. Fundamental Mechanisms in C++

Objects and Types
Strong Typing
❏ Unique type





Symbolic and literal constants
Variables
Functions
Expressions

❏ Type of functions: prototypes (as in ANSI C)
❏ Type of an expression
◆ Can be uniquely determined from the type of its
components (operands)
© 1996, 2003 – Jean-Paul RIGAULT — Version 3.1

2-3



Advanced C++ Programming

2. Fundamental Mechanisms in C++

Objects and Types
Strong Typing... but Implicit Conversions
❏ Standard conversions
◆ Standard conversions as in C and ANSI C
◆ Standard conversions related to inheritance
❏ User-defined conversions
◆ Conversions associated with constructors
Class::Class(Type);

// Type → Class

◆ Conversion operators
Class::operator Type() const;
// Class → Type
© 1996, 2003 – Jean-Paul RIGAULT — Version 3.1

2-4


Advanced C++ Programming

2. Fundamental Mechanisms in C++

Objects and Types
Strong Typing Makes Overloading Possible


(1)

❏ Overloading
◆ Same name for different functions
◆ The functions differ by their parameters
(number and types)
◆ The return value type plays no role
– Distinguishing two functions only from their return value type would
break the rule that says that the type of an expression is uniquely
determined by its operand types

© 1996, 2003 – Jean-Paul RIGAULT — Version 3.1

2-5


Advanced C++ Programming

2. Fundamental Mechanisms in C++

Objects and Types
Strong Typing Makes Overloading Possible

(2)

❏ Overloading resolution (highly simplified)
1. Search for an exact match (for parameter types)
1.1. Search for an ”exact exact”match
1.2. Search for an exact match by instantiating template functions

1.3. Search for an exact match after promoting char to int
(ANSI C compatibility!)
2. Search for possible standard conversions
3. Search for possible user-defined conversions

❏ The solution must be unique
◆ Impossibility and ambiguity are detected at compile-time
© 1996, 2003 – Jean-Paul RIGAULT — Version 3.1

2-6


Advanced C++ Programming

2. Fundamental Mechanisms in C++

Objects... And Objects
Different Sorts of Objects
❏ Base (built-in) types
❏ “Pure” abstract data types (no inheritance)
◆ Example: class String, class Vector...
◆ Inheritance may be used not only for subtyping
(e.g., code sharing with private inheritance)
❏ Abstract data types with inheritance
◆ Public inheritance (subtyping)
◆ Possibility of dynamic binding (virtual functions, inheritance
polymorphism)
© 1996, 2003 – Jean-Paul RIGAULT — Version 3.1

2-7



Advanced C++ Programming

2. Fundamental Mechanisms in C++

The C++ Class
What’s in a Class?
❏ A “natural” generalization of struct
❏ A model for creating instances
❏ A type definition (an Abstract Data Type)
❏ The set of its instances (the class extension)
❏ A name space
❏ An encapsulation unit
❏ A reuse unit (through composition and inheritance)
❏ An object (not really in C++!) ...
© 1996, 2003 – Jean-Paul RIGAULT — Version 3.1

2-8


Advanced C++ Programming

2. Fundamental Mechanisms in C++

The C++ Class
The “Natural” Generalization of struct

(1)


❏ A class is a like a C++ struct with additions






member-functions
access control
initialization/cleaning up control
inheritance
In fact, a struct in C++ is just a class, but
– all its members are public by default, inheritance level is also public
by default
– thus a C++ struct may have member-functions, constructors,
destructor, operators, private members...

© 1996, 2003 – Jean-Paul RIGAULT — Version 3.1

2-9


Advanced C++ Programming

2. Fundamental Mechanisms in C++

The C++ Class
The “Natural” Generalization of struct

(2)


❏ What about C union in C++?
◆ They are defined as a struct where all the members
have the same address
◆ They can have constructors and destructors and
(non static) member-functions
◆ Note that a C++ class with constructors or destructor
cannot be a member of an union
– Since the type of the current member of the union is not memorized,
the compiler would not know what to destroy...
© 1996, 2003 – Jean-Paul RIGAULT — Version 3.1

2-10


×