Sie sind auf Seite 1von 5

C++ Crib-Card

A type is the description of an OO entity (e.g. an integer or a car) An object is an instance of a type (e.g. my_int, my_car) A declaration says what it it called. It introduces a named type or object e.g. class someType; (forward declaration name only) int i; onotherType myObject; (instance declaration) A forward declaration A definition says what it looks like. It tells the compiler what an entity contains so it can calculate its size. E.g. typedef int my_int_type; class someType {bool some_funtion(); int i; float f;}; An implementation says how it does it. It is the collection of statements that tells the complier what you want to do. E.g. bool some_function() {return true;} An expression is something that returns a value, e.g. 72, a, a+b, a==b, strlen(my_string) A statement is an expression where the return value is unused. It is always terminated by a semicolon. e.g. a=b+1; ++i; int i=6; strcat(my_string,your_string);

C++ Punctuators
() Parenthesis group parts of a statement and surround tests. e.g. if(a==b), while(a==b), switch(myTestVal) for(int i=0;i<5;i++) {} Braces define scope and group statements. They are also used to specify aggregate initialisers. Aggregate initialisers are used for arrays & enums and can be used for structs and classes if there are no constructors.

Array Initialisation requires = and an instance, so can only be done on global or static arrays. Classes, Structs and enums are defined, not initialised with {} therefore no = is used. {} are required in a switch statement to enclose its body so a break knows where to continue from. [] Square brackets are used for indexing and to indicate an array. <myType> Angled brackets are used to specify types for template classes and system files in #include. , Commas separate items in a list, e.g. an argument list, array initialisation list or constructor initialisation list. : Colons are used after labels, such as after the case part of a switch statement and to separate parts of a statement, such as in the ternary operator '?'and before a constructor initialisation list. . -> Access object parts. e.g. myObject.myField or myObjectPointer->myField ; Semicolons terminate expressions and make them into statements. In the for statement expressions are used but not returned whereas the ternary expressions are returned. e.g. a=b; myFn(3); for(i=0;i<5;i++)... a = (b>c)?i++:j-6; Some declarations (incl. struct, enum & class) may be followed by a list of named entities. So all declarations must be terminated by a ; after any closing }. e.g. int i=8; int i,j,k; enum status{good,bad}burnerOK,flueOK,pumpOK; A function implementation (definition) returns a value, so does not have a ; after the closing } '' and \ Single quotes indicate a character literal, \ for escape chars, e.g. 'a', '\n', '\\' "" Double quotes indicate a string literal, e.g. "a string".

:: Double colon is the scope operator. ! % ^ ~ * + = / | ? Various operators. * & Pointer (address of an object) and Reference (an alias for the object pointed to). & is also used to get the address of an object. # Pre-processor directive, e.g. #include, #define.

References, pointers and address operator


The compiler doesnt care about spaces, but humans do!
Declarations use spaces : this is a pointer called ; this is a reference called
int * myIntPtr = 0; // int pointer called myIntPtr int & myIntRef = &myInt; // int ref called myIntRef

Operations attach to the operand:


myIntPtr = &myInt; // myIntPtr = address of myInt *myIntPointer = 55; // at myIntPointer = 55 myIntRef = 66; // using the reference as an alias

References are constant pointers: they always point to the same object, and can never point anywhere else. Therefore they must be initialised when declared. A variable name is, in fact, a reference. It is a pointer to the variable, which will always point to that variable. Variable names and references are used in the same way you do not need to dereference them. They are both used as an alias for the variable itself (which is the contents of the memory pointed to). When passing an argument to a function asking for a pointer, you must pass the address of the object using &myArg. When passing an argument to a function asking for a reference, you simply pass the object. The compiler will extract the address. Returning a reference from a function allows chaining.

Nested Class / Structure


Declare the nested class inside its parent in the normal way. If necessary, make forward references to later class declarations (class myClass;). To define the class functions later, nest the class identifiers: void myParentClass::myNestedClass::myFunction(){;} Inside the function definition, there is no need to use identifiers to reference other nested class variables or functions. You cannot use a forward declaration of a nested class if the compiler needs to know the structure of the forward declared class, e.g. if the outer class contains an instance of the inner class (not just a pointer to an instance), then the outer class needs the full definition of the inner class. But the inner class cannot be defined before the outer class, since its scope includes the outer class. A forward reference to the outer class before the inner class definition is not sufficient. In this case, the inner class must be fully defined within the definition of the outer class. To use the nested class object simply use the objects name and dot notation.

Memory allocation for Objects and Methods


Each class is allocated some memory which contains the implementation code for all the methods. The compiler converts a method call into a jump to the relevant memory location. Each object is alocated memory for a v-pointer (if any of the class methods are declared virtual) followed by the class attribute values.

Bitfield
If you want easy access to individual bits, use a special struct syntax. You can have any number of bits, grouped any way you like.
struct myBitfield { signed flag : 2; unsigned b2 : 6; unsigned b3 : 42; };

Union
Allows multiple objects to be mapped to the same memory space. If you want a single memory location to act as a pointer to incompatible types, it is much safer to use a union than a void pointer.
union myUnion { myBitfield uBF; long uLong; myClass * uClassPtr; } myUnionInst; myUnionInst.uLong = 0; myUnionInst.uBF.flag = -1; myUnionInst.uBF.b3 = 15; If (myUnionInst.uLong == allSet)

Das könnte Ihnen auch gefallen