C++ relearning
typedef
use typedef to give a specific type a nickname
typedef int feet;
feet distance;//completely equate 'int distance;'
enum
It is used to declare some constant quantities. It’s more than useful in defying some specific constants.
enum color { red, green, blue } c;
c = blue;
// In the default state, these constants are valued from 0,1,2...
// If we do give some constant a initial value, then comes the following circumstances.
enum color { red, green=5, blue };
// That means red == 0, green == 5, blue == 6 (blue is defaultedly valued green + 1)
extern
define a variable ,then declare this variable in different subprograms.
extern int a;
int a;
static
‘static’ is used to order the compiler to keep the variable alive, so that the variable will not be created and destroyed (its value won’t change) during the process of calling the function.
When it’s used to declare a global variable, the specific variable can only be used in the programme.
static int count = 10;
register
‘register’ is used to declare variables in the computer register, not in the RAM. The variable can not be applied using ‘&’(because it’s not in the memory).
computer register can only be used to store variables that needs quick access.
Notes: When using ‘register’ to declare a variable, the variable is stored in the register is not a must, it actually depend on the limitation of hardware and implement.
register int a;
thread
thread 线程
default 默认
pointer
A pointer’s value is the address of the variable.
//when there is not a specific address for the pointer to store. It's a good idea to let the pointer point to NULL(a special constant valued 0)
int *ip = NULL;
//* : get the value of a pointer
//& : get the address of a variable
int var = 20;
ip = &var;
cout << ip << endl << *ip << endl;
//move sizeof(int) btye address(eg: before: ip=1000, after : ip=1004)
ip++;
//a pointer and an array
int arr[10];
//the following two ways are equivilent
ip = &arr[0];
ip = arr;
//pass an array pointer parameter
int GetContent(int *arr);
int GetContent(int arr[]);
//ptr:an address; *ptr: a variable
char *ptr = "fdhsjkhfdaskdfa";
cout << ptr << endl << *ptr << endl;
Classes and inheritance
class is just like struct,
here is the difference:
During the process as inheritance,
class default as private while struct default as public