Core C++

Redefined Types

typedef


Example 1

// Define a 'TReal' type to be a double
typedef double TReal;

Example 2

// Define a type 'TIntArray' as an array of 10 integers
typedef  int TIntArray[10];

Example 3

// Combine typedef with struct — common in windows and old C code
typedef struct SMyStruct {
    int miInteger;
    double mdDouble;
} TSomeType;

Example 4

// Define a function pointer type for a function taking and returning an int
typedef int (*TFnPtr)(int);

Example 5

// Define a type for an array of four pointers
typedef double* TPtrArray[4];

Example 6

// Define a type for a pointer to a pointer
typedef int** TPtrPtr;

Example 7

// Define a type for a member function pointer
typedef int CMyClass::*TMemberFnPtr(int);

Example 8

// Define a class type as another type
typedef CMyClass TNewType;

enum


Video Tutorials

Lesson 26: Enumerations

Example 1

// Define an enumeration of months with the integers 0–11
enum EMonth {   keJanuary, keFebruary, keMarch, keApril, keMay, keJune,
                keJuly, keAugust, keSeptember, keOctober, keNovember, keDecember };

Example 2

// Define an enumeration of the first five prime numbers.
// Note that not defining the value, like three, means that
// the constant takes the value of the prior constant plus one.
enum EPrime {   keTwo = 2, keThree, keFive = 5, keSeven = keFive + 2, keNine = keSeven + 2 };

Example 3

// Define an enumeration of the first five Fibonacci numbers
// Note that successive Fibonacci numbers are the of the last two
// Note also that F1 = F2 = 1 is a repeated value
enum EFibonacci { keF0 = 0, keF1 = 1, keF2 = keF0 + keF1, keF3 = keF1 + keF2, keF4 = keF2 + keF3 };

Example 4

// Define an enumeration of flag bits
enum EBitFlag { keBit0 = 1, keBit1 = 2, keBit2 = 4, keBit3 = 8 };

Example 5

// Define an enumeration of flag bits
enum EBitFlag { keBit0 = 1 << 0, keBit1 = 1 << 1, keBit2 = 1 << 2, keBit3 = 1 << 3 };

Example 6

// Define an enumeration for directions:
// left = 0, right = 1, up = 4, down = 5
enum EDirection { keLeft = 0, keRight, keUp = 4, keDown };

// Declare and initialize
EDirection eMyDirection = keUp;

// Convert enum to an int value (automatic)
int iMyInt = eMyDirection;

// Convert int to enum
int iOne = 1;
eMyDirection = (EDirection)iOne;

// Conversion from an int might not always make sense
int iSix = 6;
eMyDirection = (EDirection) iSix;
 

© 2007–2024 XoaX.net LLC. All rights reserved.