Codec ░ c

313Swimming Nearly drowning in C++

OK, I’ve decided to do it. To leave the elegance of Objective-C and have a closer look at C++. After all, isn’t everyone doing oF these days?

Here a recapitulation of usesful C and some quick notes on C++ peculiarities – sorry – features: (That might be a good place to start.)

Initialisation
int a = 1; // standard c way
int b(1); // same

Defined constants
#define PI 3.1415927 // faster than var

Declared constants
const int c = 100 // immutable

Assignment
= can also be used as rvalue
a = 1 + (b = 2);

same as:
b = 2;
a = 1 + b;

Comma operator
a = (b=3, b+2); // a = 5

When the set of expressions has to be evaluated for a value, only the rightmost expression is considered

Type casting
int i, j;
float f = 1.234;
i = (int) f;
j = int (f);

sizeof()
One parameter, either a type or a variable and returns the size in bytes.

a = sizeof (char);

Will become important with arrays.

305Converting float to string %4.2f 107Typedef, Struct, Enum

305Converting float to string %4.2f

printf ("floats: %4.2f %+.0e %E \n", 3.1416, 3.1416, 3.1416);

floats: 3.14 +3e+000 3.141600E+000

376Variable length of accuracy of float in NSString 313Swimming Nearly drowning in C++ 107Typedef, Struct, Enum 73Concatenating Strings @”string1″ @”string2″ @”stringN” 33CGPointMake

107Typedef, Struct, Enum

struct
a collection of types, composite data type, composition, “user-definded data structure”
struct Test {
char* name;
int nr;
float f;
};

Name of type is “struct Test”, and not “Test” alone.
struct Test myNewTest

Semicolon after closing parethesis is essentional.

Access composed types with dot-syntax:
myNewTest.name = "hjkjnk";
myNewTest.nr = 32;
myNewTest.f = 2.2424
;

typedef
a shorthand, an alias for a type, for ease of readability.

typedef int MyType

“MyType n;” would be equal to “int n;”

enum
“introduces a symbolic name for an integer constant”

enum direction_enum {LEFT, RIGHT};
/* LEFT -> 0, RIGHT -> 1; */
enum direction_enum x = LEFT;

typedef enum

typedef enum {
LEFT;
RIGHT;
} direction;

declatation:
-(void)myFunction:(direction)LEFTorRIGHT;

call:
-(void)myFunction:LEFT;

typedef struct

typedef struct objectInfo_ {
CGPoint origin;
NSString* name;
NSColor* color;
} objectInfo

Shortcut. Instead of writing ’struct objectInfo_’, write ‘objectInfo’. The keyword ‘objectInfo_’ seems to be optional.

313Swimming Nearly drowning in C++ 305Converting float to string %4.2f