Programmer's Guide
Total Page:16
File Type:pdf, Size:1020Kb
3.1 PROGRAMMER'S GUIDE • LANGUAGE STRUCTURE • CLASS LIBRARIES • ADVANCED PROGRAMMING TECHNIQUES • ANSI CIMPLEMENTATION BORLAND Borland C++ Version 3.1 Programmer's Guide BORLAND INTERNATIONAL INC. 1800 GREEN HILLS ROAD P.O. BOX 660001, SCOTTS VALLEY, CA 95067-0001 Copyright © 1991, 1992 by Borland International. All rights reserved. All Borland products are trademarks or registered trademarks of Borland International, Inc. Other brand and product names are trademarks or registered trademarks of their respective holders. Windows, as used in this manual, refers to Microsoft's im plementation of a windows system. PRINTED IN THE USA. Rl 10 9 8 7 6 5 4 c o N T E N T s Introduction 1 Constants and internal What's in this book .................... 1 representation ................... 19 An introduction to the formal definitions. 2 . Constant expressions .. .. 20 Syntax and terminology ............. 3 Punctuators ....................... 21 Brackets ........................ 21 Chapter 1 Lexical elements 5 Parentheses ..................... 21 Whitespace .......................... 6 Braces .......................... 21 Line splicing with \ ................. 6 Comma ......................... 22 Comments ......................... 7 Semicolon. .. 22 C comments . .. 7 Colon .......................... 23 Nested comments ................. 7 Ellipsis ......................... 23 C++ comments ................... 8 Asterisk (pointer declaration) ...... 23 Comment delimiters and whitespace . 8 Equal sign (initializer) ............ 23 Tokens .............................. 8 Pound sign (preprocessor directive) .24 Keywords .......................... 9 Identifiers . .. 10 Chapter 2 Language structure 25 Naming and length restrictions .... 10 Declarations ......................... 25 Identifiers and case sensitivity ..... 10 Objects ........................... 25 Uniqueness and scope ............ 11 Lvalues ........................... 26 Constants . .. 11 Rvalues ......................... 27 Integer constants . .. 11 Types and storage classes ........... 27 Decimal constants . .. 11 Scope ............................ 27 Octal constants ................ 12 Block scope ..................... 28 Hexadecimal constants ......... 13 Function scope .................. 28 Long and unsigned suffixes ..... 13 Function prototype scope ......... 28 Character constants .............. 14 File scope ....................... 28 Escape sequences .............. 14 Class scope (C++) ................ 28 Borland C++ special two-character Scope and name spaces ........... 28 constants ..................... 15 Visibility. .. 29 Signed and unsigned char ....... 15 Duration .......................... 29 Wide character constants ........ 16 Static duration ................... 29 Floating-point constants .......... 16 Local duration . .. 30 Floating-point constants-data Dynamic duration ............... 30 types ......................... 16 Translation units ................... 31 Enumeration constants ......... 17 Linkage ........................... 31 String literals ... .. 18 N arne mangling ................. 32 Declaration syntax ................... 33 Declarations and definitions ......... 60 Tentative definitions ............... 33 Declarations and prototypes . .. 61 Possible declarations ............... 34 Definitions ........................ 63 External declarations and definitions . 36 Formal parameter declarations. .. 64 Type specifiers .................... 38 Function calls and argument Type taxonomy . .. 38 conver~ons ....................... 64 Type void . .. 39 Structures . .. 65 The fundamental types ............. 39 Untagged structures and typedefs .... 66 Integral types .................... 40 Structure member declarations ...... 66 Floating-point types .............. 41 Structures and functions ............ 67 Standard conversions . .. 41 Structure member access . .. 67 Special char, int, and enum Structure word alignment . .. 69 conver~ons ..................... 42 Structure name spaces .............. 69 Initialization ...................... 42 Incomplete declarations ............. 70 Arrays, structures, and unions ..... 43 Bit fields . .. 70 Simple declarations ................ 44 Unions ............................. 71 Storage class specifiers . .. 45 Anonymous unions (C++ only) ...... 72 Use of storage class specifier auto .. 45 Union declarations ................. 73 Use of storage class specifier extern. 45 Enumerations ....................... 73 Use of storage class specifier Expressions ......................... 75 register ......................... 45 Expressions and C++ ............... 78 Use of storage class specifier static .. 46 Evaluation order ................... 78 Use of storage class specifier Errors and overflows ............... 79 typedef ......................... 46 Operator semantics .................. 79 Modifiers ......................... 47 Operator descriptions ................ 79 The const modifier ............... 47 Unary operators ............. ,..... 81 The interrupt function modifier .... 49 Binaryoperators ................... 81 The volatile modifier ............. 49 Additive operators . .. 81 The cdecl and pascal modifiers . .. 50 Multiplicative operators .......... 81 pascal ........................ 50 Shift opera tors . .. 81 cdecl ......................... 50 Bitwise operators ................ 81 The pointer modifiers ............. 51 Logical operators ................ 81 Function type modifiers . .. 52 Assignment operators ............ 81 Complex declarations and declarators .53 Relational operators .............. 82 Pointers ............................ 54 Equality operators ............... 82 Pointers to objects . .. 55 Component selection operators .... 82 Pointers to functions ............... 55 Class-member operators .......... 82 Pointer declarations ................ 56 Conditional operator ............. 82 Pointers and constants . .. 57 Comma operator ................. 82 Pointer arithmetic . .. 58 Postfix and prefix operators ......... 82 Pointer conversions ................ 59 Array subscript operator [ ] ........ 82 C++ reference declarations .......... 59 Function call operators () ......... 83 Arrays .............................. 59 Structure/union member operator Functions ........................... 60 . (dot) .......................... 83 Structure/union pointer Blocks 98 operator -> ..................... 83 Labeled statements ................ 98 Postfix increment operator ++ ..... 84 Expression statements ............. 99 Postfix decrement operator -- ..... 84 Selection statements ............... 99 Increment and decrement operators .. 84 if statements ................... 99 Prefix increment operator ......... 84 switch statements .. , ........... 100 Prefix decrement operator .....- ... , 84 Iteration statements .............. 101 Unary operators ................... 85 while statements ............... 101 Address operator & .............. 85 do while statements ............ 101 Indirection operator * ............ , 86 for statements ................. 102 Unary plus operator + ............ 86 Jump statements ................. 103 Unary minus operator - .......... , 86 break statements ............... 103 Bitwise complement operator ~ .... 86 continue statements ............ 103 Logical negation operator! ........ 86 goto statements ................ 103 The sizeof operator. .. 87 return statements .............. 104 Multiplicative operators ............ 87 Additive operators ................. 88 Chapter 3 C++ specifics 105 The addition operator + ........... 88 Referencing ........................ 105 The subtraction operator - ........ 89 Simple references ................. 106 Bitwise shift operators ....... .. 89 Reference arguments .............. 106 Scope access operator ............... , 108 Bitwise shift operators «< and ») . 89 Relational operators ................ 90 The new and delete operators ........ 108 The less-than operator < .......... 90 Handling errors .............,..... 109 The greater-than operator> ....... 91 The operator new with arrays ...... 109 The less-than or equal-to operator The operator delete with arrays .. , .. 109 The ::operator new ................ 110 <= ... ~ ......................... 91 The greater-than or equal-to Initializers with the new operator ... 110 operator >= ..................... 91 Classes ............................ 111 Equalityoperators ................. 91 Class names . .. 111 The equal-to operator == .......... 91 Class types . .. 111 The inequality operator != ......... 92 Class name scope ................. 112 Class objects ..................... 113 Bitwise AND operator & ............ 92 Bitwise exclusive OR operator /\ ..... 93 Class member list. .. 113 Bitwise inclusive OR operator I ...... 93 Member functions ................ 113 The keyword this ................. 113 Logical AND operator && .......... 93 Logical OR operator I I ............. 94 Inline functions . .. 114 Conditional operator? : . .. 94 Static members ................... 115 Assignment operators .............. 95 Member scope . .. 116 The simple assignment operator = .. 95 Nested types ................... 117 The compound assignment Member access control. .. 118 operators ....................... 96 Base and derived class access ....... 120 Comma operator ......... , .. , ..... , 96 Virtual base classes .................. 122 C++ operators ..................... 97 Friends of classes ................... 122 Statements .......................... 97 Constructors and destructors ........