Road C ++ learning --2

One C + + Correct C language Speech of \ Color {pink} {a, C ++ language enhancements C}

1, global variable detection enhancement

int a;
int a=10;

2, the detection enhancement function, enhanced parameter types, the return value detection enhancement, detection enhancement function call parameters

int getRectS(int w, int h)
{
	return w*h
}

3, detection enhancement type conversion

void test03()
{
	char *p = (char*)malloc(sizeof(64)); //malloc返回值是void*
}

4, struct enhanced

struct Person
{
	int m;
	void plusAge(); //c++中struct可以加函数,使用时可以不加struct关键字
}

5, bool Type Enforcement

bool flag; //非0值会转换成1

6, ternary operator

void test05()
{
 int a=100;
 int b=200;
 cout << "ret=" << (a < b ? a : b) << endl;
(a < b ? a : b) = 1000;
}

7, const enhanced default internal links

void test07()
{
 const int m=30;
 int *p = (int *)
}

. 8, taken const addresses allocated temporary memory, the compiler will allocate memory extern const to a variable, ordinary const variable initialization, the custom data types
Here Insert Picture Description

\ Color {pink} {II} references

  • The basic syntax Type & formerly known as Alias ​​=
  • Reference must be initialized
  • Reference can not be modified after initialization.
  • Reference to the array
    Here Insert Picture Description
typedef int(ARRAYREF)[10]; //一个具有10个元素的int类型的数组
ARRAYREF & pArr2 = arr;

Writes the left called & quote, write to the address on the right called to take

Parameter passing

  • Value transfer
  • Delivery address
  • Passed by reference
    • Precautions
      • Must refer to a valid memory space // int & a = 10;
      • Do not return a reference to a local variable
      • If a function's return value is a reference, then the calling function can be used as an lvalue
        Here Insert Picture Description

Reference is essentially a pointer constant
constant references

const int &ref = 10;
int * p = (int*)&ref;
*p = 1000;

Guess you like

Origin blog.csdn.net/weixin_43615373/article/details/90294535