Follow Techotopia on Twitter

On-line Guides
All Guides
eBook Store
iOS / Android
Linux for Beginners
Office Productivity
Linux Installation
Linux Security
Linux Utilities
Linux Virtualization
Linux Kernel
System/Network Admin
Programming
Scripting Languages
Development Tools
Web Development
GUI Toolkits/Desktop
Databases
Mail Systems
openSolaris
Eclipse Documentation
Techotopia.com
Virtuatopia.com
Answertopia.com

How To Guides
Virtualization
General System Admin
Linux Security
Linux Filesystems
Web Servers
Graphics & Desktop
PC Hardware
Windows
Problem Solutions
Privacy Policy

  




 

 

Thinking in C++ Vol 2 - Practical Programming
Home Next
Thinking In C++

Volume 2: Practical Programming

Bruce Eckel, President, MindView, Inc.
Chuck Allison, Utah Valley State College


 



Table of Contents

Introduction 1

Goals.......................................... 1

Chapters..................................... 2

Exercises..................................... 5

Exercise solutions............... 5

Source code................................. 5

Compilers.................................... 7

Language standards..................... 9

Seminars, CD ROMs
& consulting................................. 9

Errors....................................... 10

About the cover......................... 10

Acknowledgements..................... 10

I: Building Stable Systems 13

 

1: Exception Handling 15

Traditional error handling............. 16

Throwing an exception................ 18

Catching an exception................. 20

The try block.................... 20

Exception handlers........... 20

Termination
and resumption................ 22

Exception matching..................... 23

Catching any exception..... 25

Rethrowing an exception... 26

Uncaught exceptions......... 26

Cleaning up................................ 28

Resource management..... 30

Making everything
an object......................... 32

auto_ptr.......................... 35

Function level try blocks... 36

Standard exceptions................... 38

Exception specifications............... 40

Better exception
specifications?.................. 45

Exception specifications
and inheritance................ 46

When not to use
exception specifications..... 47

Exception safety......................... 48

Programming with exceptions....... 52

When to avoid exceptions.. 52

Typical uses of exceptions. 54

Overhead................................... 58

Summary................................... 60

Exercises................................... 61

2: Defensive Programming 63

Assertions................................. 66

A simple unit test framework........ 70

Automated testing............ 71

The TestSuite Framework.. 75

Test suites....................... 79

The test framework code... 81

Debugging techniques................. 87

Trace macros................... 87

Trace file......................... 88

Finding memory leaks....... 90

Summary................................... 96

Exercises................................... 97

 

 

II: The Standard C++ Library 101

 

3: Strings in Depth 103

What s in a string?.................... 104

Creating and initializing
C++ strings............................. 106

Operating on strings................. 109

Appending, inserting, and
concatenating strings...... 110

Replacing string
characters...................... 112

Concatenation using nonmember
overloaded operators...... 117

Searching in strings.................. 117

Finding in reverse........... 123

Finding first/last of
a set of characters.......... 124

Removing characters
from strings................... 126

Comparing strings.......... 129

Strings and
character traits............... 134

A string application................... 140

Summary................................. 145

Exercises................................. 146

4: Iostreams 151

Why iostreams?........................ 151

Iostreams to the rescue............ 156

Inserters and extractors.. 156

Common usage.............. 161

Line oriented input......... 164

Handling stream errors.............. 165

File iostreams........................... 168

A File Processing
Example........................ 169

Open modes................... 171

Iostream buffering.................... 173

Seeking in iostreams................. 175

String iostreams....................... 179

Input string streams........ 180

Output string streams...... 182

Output stream formatting.......... 186

Format flags................... 186

Format fields.................. 188

Width, fill, and precision.. 190

An exhaustive example... 191

Manipulators............................. 194

Manipulators with
arguments..................... 196

Creating manipulators..... 199

Effectors........................ 201

Iostream examples.................... 203

Maintaining class library
source code................... 204

Detecting compiler errors 208

A simple data logger....... 211

Internationalization................... 216

Wide Streams................. 216

Locales.......................... 218

Summary................................. 221

Exercises................................. 222

5: Templates in Depth 227

Template parameters................. 227

Non type
template parameters....... 228

Default template
arguments..................... 230

Template template
parameters.................... 232

The typename keyword... 238

Using the template
keyword as a hint........... 240

Member Templates......... 242

Function template issues........... 245

Type deduction of function
template arguments........ 245

Function template
overloading.................... 249

Taking the address
of a generated
function template............ 251

Applying a function
to an STL sequence......... 255

Partial ordering of
function templates.......... 259

Template specialization............... 260

Explicit specialization....... 261

Partial Specialization....... 263

A practical example........ 265

Preventing template
code bloat...................... 268

Name lookup issues.................. 273

Names in templates........ 273

Templates and friends..... 279

Template programming idioms.... 285

Traits............................. 285

Policies.......................... 291

The curiously recurring
template pattern............. 294

Template metaprogramming....... 297

Compile time
programming................. 298

Expression templates...... 308

Template compilation models...... 315

The inclusion model........ 315

Explicit instantiation........ 316

The separation model...... 319

Summary................................. 320

Exercises................................. 321

6: Generic Algorithms 325

A first look............................... 325

Predicates...................... 329

Stream iterators............. 331

Algorithm complexity...... 333

Function objects....................... 335

Classification of
function objects.............. 336

Automatic creation of
function objects.............. 338

Adaptable function objects 341

More function
object examples............. 343

Function pointer adaptors 351

Writing your own
function object adaptors.. 358

A catalog of STL algorithms....... 362

Support tools for
example creation............ 365

Filling and generating...... 368

Counting........................ 370

Manipulating sequences... 372

Searching and replacing.. 377

Comparing ranges.......... 385

Removing elements........ 389

Sorting and operations
on sorted ranges............ 393

Heap operations............. 403

Applying an operation to
each element in a range.. 405

Numeric algorithms......... 413

General utilities.............. 417

Creating your own
STL style algorithms................. 419

Summary................................. 420

Exercises................................. 421

7: Generic Containers 429

Containers and iterators............ 429

STL reference
documentation................ 431

A first look............................... 432

Containers of strings....... 438

Inheriting from
STL containers................ 440

A plethora of iterators............... 442

Iterators in
reversible containers....... 445

Iterator categories.......... 446

Predefined iterators........ 448

The basic sequences:
vector, list, deque..................... 454

Basic sequence operations 454

vector............................ 457

deque............................ 465

Converting between
sequences...................... 467

Checked random access. 470

list................................. 471

Swapping sequences....... 477

set.......................................... 479

A completely
reusable tokenizer.......... 482

stack....................................... 487

queue...................................... 491

Priority queues......................... 496

Holding bits.............................. 506

bitset<n>....................... 507

vector<bool>................. 511

Associative containers............... 513

Generators and fillers
for associative containers 518

The magic of maps......... 521

Multimaps and
duplicate keys................ 523

Multisets........................ 527

Combining STL containers.......... 530

Cleaning up
containers of pointers............... 534

Creating your own containers..... 536

STL extensions......................... 538

Non STL containers.................. 540

Summary................................. 546

Exercises................................. 546

III: Special Topics 549

 

8: Runtime Type Identification 551

Runtime casts.......................... 551

The typeid operator.................. 557

Casting to
intermediate levels......... 560

void pointers.................. 561

Using RTTI
with templates................ 562

Multiple inheritance.................... 563

Sensible uses for RTTI............... 564

A trash recycler.............. 565

Mechanism and
overhead of RTTI...................... 570

Summary................................. 570

Exercises................................. 571

9: Multiple Inheritance 573

Perspective............................... 573

Interface inheritance.................. 575

Implementation inheritance........ 579

Duplicate subobjects................. 585

Virtual base classes................... 589

Name lookup issues.................. 599

Avoiding MI.............................. 603

Extending an interface............... 603

Summary................................. 608

Exercises................................. 609

10: Design Patterns 613

The pattern concept.................. 613

Prefer composition
to inheritance................. 615

Classifying patterns................... 615

Features, idioms,
patterns......................... 616

Simplifying Idioms..................... 617

Messenger..................... 617

Collecting Parameter....... 618

Singleton................................. 619

Variations on Singleton.... 621

Command: choosing
the operation........................... 626

Decoupling event handling
with Command............... 628

Object decoupling..................... 631

Proxy: fronting for
another object................ 632

State: changing
object behavior.............. 634

Adapter................................... 636

Template Method....................... 639

Strategy: choosing the
algorithm at runtime.................. 640

Chain of Responsibility:
trying a sequence of strategies... 642

Factories: encapsulating
object creation......................... 645

Polymorphic factories...... 647

Abstract factories............ 651

Virtual constructors......... 654

Builder: creating
complex objects....................... 660

Observer.................................. 667

The inner class idiom.... 671

The observer example.... 674

Multiple dispatching................... 679

Multiple dispatching
with Visitor..................... 683

Summary................................. 687

Exercises................................. 688

11: Concurrency 691

Motivation................................ 692

Concurrency in C++.................. 694

Installing ZThreads......... 695

Defining Tasks.......................... 696

Using Threads.......................... 698

Creating responsive
user interfaces............... 700

Simplifying with
Executors....................... 702

Yielding.......................... 706

Sleeping........................ 707

Priority.......................... 709

Sharing limited resources........... 711

Ensuring the
existence of objects........ 711

Improperly accessing
resources....................... 715

Controlling access........... 719

Simplified coding
with Guards.................... 721

Thread local storage....... 724

Terminating tasks...................... 727

Preventing iostream
collision......................... 727

The ornamental garden... 728

Terminating
when blocked................. 733

Interruption.................... 735

Cooperation between threads..... 741

Wait and signal............... 742

Producer consumer
relationships................... 747

Solving threading problems
with queues.................... 750

Broadcast...................... 757

Deadlock.................................. 764

Summary................................. 770

Exercises................................. 773

A: Recommended Reading 777

General C++............................ 777

Bruce s books................. 777

Chuck s books................ 779

In depth C++.......................... 779

Design Patterns........................ 781

B: Etc 783

 

Index 791

 

 


Introduction

In Volume 1 of this book, you learned the fundamentals of C and C++. In this volume, we look at more advanced features, with an eye towards developing techniques and ideas that produce robust C++ programs.

We assume you are familiar with the material presented in Volume 1.

Thinking in C++ Vol 2 - Practical Programming
Home Next

 
 
   Reproduced courtesy of Bruce Eckel, MindView, Inc. Design by Interspire