C + + Basics

Process oriented
object-oriented

Development of C + + language

In 1979, Benjamin straw, the father of C + +, developed C with class
c language with classes
C language is a subset of C + + language. C + + language is an extension of C language
C + + was officially named in 1983
In 1985, CFront1.0 was the first commercial C + + compiler
1987 GNU C++
1990 Borland C++
1992 Microsoft C++, IBM C++
1998 ISO C++98
2003 ISO C++03
A large number of new functions of ISO C++2011/C++11/C++0x in 2011
2014/2017 C + + adds many new features
c java python c++

The stability of the version is very important

C + + features

1. The running speed is slower than C language and much faster than java/python
2. The development efficiency of C language is low, and the development efficiency of C + + is very high
Both operation speed and development efficiency
#Application fields of C + + language
1. Game development: strong modeling ability and high-performance cocos2d-x game engine
Netease Jufeng electric soul winger
2. Scientific computing C + + algorithm library
3. Network and distributed: ACE framework
4. Desktop application: VC/MFC VC++6.0 Microsoft C + + compiler
MFC graphical interface development kit
QT C + + syntax
5. The C + + compiler optimized by operating system and device driver makes the execution efficiency of C + + comparable to that of C
6. Mobile terminal
7. Military and aerospace

C + + is richer than C language

1.C + + supports object-oriented programming, macro object-oriented and micro process oriented
2. Support generic programming (type parameterization)
3. Support exception mechanism
C language error handling and normal logic are the same code
4. Support operator overloading
5.C + + provides rich standard libraries and rich third-party libraries

Use of C + +

1.C++Compiler for g++,If used gcc Compile, need to bring-lstdc++
	Specify the standard it uses C++Runtime for
2.Source file extension: .cpp  .cc  .C  .cxx  .c++
	Best use.cpp
3.Header file #Include < iostream > iostream IO stream
	C++There are generally no standard library header files in.h
	Yes, of course.h   
	C++The contents of the standard library are placed in a std In namespace
	using namespace std;
4.cout Object standard output object
  cin  Object standard input object
  cerr Object standard error output object
  endl Object wrap object
  <<: output operator    cout  cerr
  >>: Input operator   cin

Namespace namespace

1.C++Identifier in program(Type, function, variable)Divide into several groups according to some logic
 Logical division of identifiers
cin cout  endl C++The functions, objects and types of the standard library are located in std In namespace
 Avoid name conflicts
2.Syntax for defining namespaces
namespace Name of namespace{
global variable
 type definition
 function
}
3.Use namespaces
(1)Scope qualifier namespace name::identifier 
	Indicates that explicit access is a specific identifier in a specific namespace
	Although the most troublesome,But it can be accessed accurately
#include <iostream>
int  main(){
	std::cout << "Hello world!" << std::endl;
	std::cout << 1919 << std::endl;
	std::cout << 3.14 << std::endl;
	std::cout << 2011 << "," << 1314 << "," << std::endl;
	return 0;	
}

There is no use method with using namespace std

(2)Namespace instruction
	using namespace Namespace name;
	The statement can be anywhere  
	Indicates that after the instruction, all identifiers in the namespace to which the instruction refers are visible to the current scope
	Next, you can directly access the identifier in the namespace without adding"Namespace name::"
	Of course, it can also be added
	The scope is visible to the current scope
(3)Namespace declaration
	using Namespace::identifier 
	An identifier in the namespace will be specified(member)It is introduced into the current scope and can be accessed directly
	The scope is imported into the current scope
#include <iostream>
using namespace std;

//Define namespace
namespace wd1{
	int g = 1024;
	void func(void){
		cout << "func" << endl;	
	}
	struct Stu{
		int no;
		char name[40];
	};
}


int main(){
	using wd1::g;
	cout << g << endl;
	
	cout << wd1::g << endl;
	wd1::func();
	struct wd1::Stu s = {};
	using namespace wd1;
	cout << g << endl;
	func();
	struct Stu s2 = {};
	return 0;	
}
	4.Anonymous space
	The compiler places identifiers of global areas that do not specify namespaces in anonymous namespaces
	If you want to specify access to an identifier in an anonymous namespace, use it directly  ::identifier 

	:: Scope qualifier
	Namespace name::Identifier accesses the identifier in the specified namespace
	::Identifiers access identifiers in anonymous namespaces to distinguish between global and local variables with the same name
```cpp
#include <iostream>
using namespace std;

//If a global identifier does not specify a namespace, the compiler will place it in an anonymous namespace by default
int gv = 1024;


int main(){
	cout << gv << endl;
	cout << ::gv << endl;  //Accesses the identifier in the specified namespace (anonymous namespace)
	int gv = 9527;
	cout << gv << endl;//Local priority principle for accessing local variables local variables with the same name hide global variables with the same name
	cout << ::gv << endl;//Access global
	return 0;	
}

5.Namespace merge
 In a program, if the namespace names are the same, it is considered to be the same namespace
 The identifiers inside will be merged
#include <iostream>
using namespace std;

namespace myspace{
	int x = 1024;
	void func(){
		cout << "func" << endl;	
	}
}

namespace myspace{
	int y = 9527;	
	void bar(){
		cout << "bar" << endl;	
	}
}

int main(){
	using namespace myspace;
	cout << x << endl;
	cout << y << endl;
	func();
	bar();
	return 0;	
}

6.Namespace nesting
 A namespace contains another namespace
 Not directly using namespace Inner namespace; Is not visible to the current scope

using namespace Outer namespace;
using namespace Inner namespace;
using namespace Outer namespace::Inner namespace;


using namespace std;
	int x=2;
	namespace s1{
		int x= 1024;
		namespace s2{
			int y =9527;
		}
		namespace s4{
			int w = 520;
		}
	}
	
	cout << s1::s2::y <<endl;
	using s1::s2::y;
	cout << y << endl;
	
	using namespace s1;//Visible to current scope
	using s2::y;
	!   using namespace s3;//This is wrong because s2 is not yet visible
	using namespace s1::s2::s3;
#include <iostream>
using namespace std;

//int x = 1;

namespace s1{
	int x = 1024;
	namespace s2{
		int y = 9527;
		namespace s3{
			int z = 1314;	
		}
	}
	namespace s4{
		int x = 520;	
	}
}

int main(){
	using s1::x;
	cout << x << endl;
	cout << s1::x << endl;
	//cout << y << endl;
	cout << s1::s2::y << endl;
	using s1::s2::y;
	cout << y << endl;
	using namespace s1;
	using s2::y;
	//using namespace s3;
	using namespace s1::s2::s3;
	cout << z << endl;
	using namespace s4;
	cout << x << endl;//There are x in both namespaces. Because they have no priority relationship with the current scope, there will be ambiguity
	return 0;
}

be careful:
	If used using namespace Namespace; Make namespace declarations visible to the current scope
	If two different scopes contain the same name and identifier, no access is OK
	However, if direct access is used, there will be ambiguity
#include <iostream>
using namespace std;


namespace s1{
	int x = 1111;	
}

namespace s2{
	int x = 9527;	
}


int main(){
	cout << s1::x << endl;
	cout << s2::x << endl;

	using namespace s1;
	cout << x << endl;
	using namespace s2;
//	cout << x << endl;// There will be ambiguity
	cout << s1::x << endl;
	cout << s2::x << endl;

	using s2::x; //Import to current scope
	cout << x << endl;
	cout << s1::x << endl;

	//using s1::x;// There is an X in the current scope
	

	return 0;	
}
	using namespace Namespace; There will be no ambiguity in the declaration
	using Namespace name::identifier      There may be ambiguity
	
7.Namespaces are programmed in multiple files

Structure, union and enumeration of C + +

1.structural morphology
	(1)C++When using structures in,It can be omitted directly struct keyword   Stu s ={}  sizeof(s) == 1   
	(2)sizeof(Empty structure),stay C++1 in,C 0 in
	(3)C++Functions can be defined in the structure,Functions in structures can access members directly
		C++Functions in structures,Yes C Structure can be implemented by defining function pointers
#include <iostream>
using namespace std;

//Object oriented programming idea C + + is implemented in C language. Although there is no function in C language, there can be function pointers
struct Date{
	int year;
	int mon;
	int day;
	//Defining functions inside a structure can directly access member variables in the structure
	void show(){
		cout << year << "-" << mon << "-" << day << endl;	
	}
};


int main(){
	Date d = {2021,8,24};
	d.show();//C + + is equivalent to directly optimizing the function pointer and turning it into a function
	Date d1 = {2008,8,8};
	d1.show();
	cin >> d1.year >> d1.mon >> d1.day;
	return 0;	
}

2.union
	C++Anonymous Federation is supported in
	The syntax form of union is used to describe the layout of variables in memory
	Variables in anonymous unions can be accessed directly,Joint variables do not need to be defined
#include <iostream>
using namespace std;


int main(){
	//Anonymous union is equivalent to defining two variables u1,c1  
	//Variable memory in anonymous unions is shared
	union{
		int u1;
		char c1;
	};
	
	u1 = 0x12345678;
	cout << hex << (int)c1 << endl;//Output 78

	int u2;
	char c2;
	return 0;	
}

3.enumeration
	C++Enumeration in is an independent type and cannot be directly assigned to enumeration variables with integer values
	Enumeration variables or enumeration values are still allowed to be assigned to integer variables
#include <iostream>
using namespace std;

enum Dire{UP,DO,LE,RI};

int main(){
	Dire d = UP;
	d = DO;
	//d = 0;// Enumeration variables cannot be assigned an integer value
	cout << UP << endl;//The value of UP is equal to 0, d=0; C + + is not allowed. Type checking is enhanced

	int x = UP;
	x = d;//Enumeration variables and enumeration values can be assigned to integers
	cout << x << endl;

	return 0;	
}

Boolean types in C + +

C There is no such basic data type in
C++Provided,No header file
bool Type takes up one byte  sizeof(bool)=1;
true/false 1/0
 Non zero is true 0.0 NULL '\0'

boolalpha Direct display for Boolean types true/false Not 1/0
noboolalpha close

bool a = true;
cout << boolalpha << a << endl;//So the output is true, not 1
cout << boolalpha << true << endl;

a=123;a="Hello";a=3.14 All allowed
 Any variable and any data can be assigned a non boolean variable, which is true. The result of direct output is 0/1
#include <iostream>
using namespace std;


int main(){
	bool a = false;
	cout << a << endl;//0
	a = true;
	cout << a << endl;
	cout << true << ":" << false << endl;
	//boolalpha directly displays true/false instead of 1 / 0 for Boolean types
	cout << boolalpha  << a << endl;	
	cout << boolalpha << true << ":" << false << endl;

	a = 123;
	a = "Hello";
	a = 3.14;
	a = 2;
	cout << noboolalpha << a << endl;//1
	return 0;	
}

Operator aliases in C + +

It will not be used under normal circumstances

  You can replace it directly with the following word
	&& and
	|| or
	! not
	& bitand
	^ xor
	{ <%
	} %>
	[ <:
	] :>
#include <iostream>
using namespace std;


int main(){
	int year=2021; 
	//cin >> year;
	if(year%4==0 and year%100!=0 or year%400==0){
		cout << "leap year" << endl;
	}
	int arr<:5:> = <%5,4,3,1,2%>;	
	cout << arr<:3:> << endl;	
	return 0;	
}

Functions in C + +

1. Support overloading (independent of return value)

	(Function name parameter)  (phone)
	Eat to pay for this behavior: you can use cash Alipay. 
	Under the same scope,Same function name,The parameter list is different,This constitutes a heavy load

Overload prerequisites

Equal relationship under the same scope(If the scope is different, it is overloaded,Because it will be hidden)
			Same function name
			The parameter list is different
				1.Different parameter types(Corresponding position)
				2.Different number of parameters
				3.Constant properties are different (const)  The constant properties of pointer and reference types are different
			Independent of function return type
C++in,When calling overloaded functions,The compilation stage determines the overloaded function to be called according to the number and type of parameters passed during function call
 Static runtime:In the compilation phase, determine which function to call, statically bind overloads and templates
 Dynamic runtime:Which function to call is determined at run time		Dynamic associative virtual function

Why can overloading be supported in C + +?

	link -S Generate assembly code file.s See why it makes a difference
	g++The compiler will rename the function names in the program,Merge the type information in the parameter list into the function name,To ensure the uniqueness of the function name

How to cancel this rename operation
extern "C" requirement g++The compiler does not modify functions C++Name change operation,Works on only one function,If you want all,need{}contain
	To facilitate in C Call in language C++Compile generated code
#include <iostream>
using namespace std;

extern "C"
void func(){//No,
	
}

void bar(){//Changed
	
}


extern "C"{
	void call(){}//No,
	void hello(){}//No,
}

int main(){
	
	return 0;	
}

Function call matching problem during overloading
	1.Make an exact match first,The number and type of parameters are exactly the same
	2.Pointers and references with constant attributes can overload functions with pointers and references without constant attributes
		If not const Version and const Both versions are available,Then the matching call is made according to the constant attribute when calling
		If there is only one version of the function,No constant attribute data can be called const edition,
		however const Property must not call non const Version function
		Can be automatically enhanced const attribute,But we can't lose it rashly const attribute
#include <iostream>
using namespace std;
/*
void func(int *p){
	cout << "func(int *)" << endl;	
}
*/

void func(const int *p){
	cout << "func(const int *)" << endl;	
}


int main(){
	int *p1;
	const int *p2;

	func(p1);//Auto lift const 
	func(p2);
	return 0;	
}


3.If during the matching process,There is no exact match,Parameters are implicitly converted
		If the implicit conversion process,Two or more functions were found,Can be transformed equally,There will be ambiguity
		func(char,char)
		func(int,int)
		call
		func('a',78);//You don't know which to call, because the conversion quantity is the same, just turn an argument

		int max(int a,int b){
			cout << "int,int" << endl;
			return a>b>a:b;
		}
		int max(int a,int b,int c){
			cout << "int,int,int" << endl;
			if(a>=b && a>=c)
				return a;
			...
		}
		double max(double a,double b){
			return a>b?a:b;
			cout << "double,double" << endl;
		}
		int main(){
			cout << max(1,2) << endl;
			cout << max(1,2,3) << endl;
			cout << max(3.14,3.33) << endl;
			return 0;
		}
		
------------------------------------------------
		
		void func(){
			cout << "func()" << endl;
		}
		//redefinition
		/*
		void func(){
			cout << "func()" << endl;
		}
		*/
	   
	   //Ambiguity between old and new definitions
	   /*
	   int func(){  
		   return 1;
	   }
	   */
	  
	  void func(int a){
		  cout << "func(int)" << endl;
	  }
	  
	  void func(double b){
		  cout << "func(double)" << endl;
	  }
	  
	  void func(char a,char b){
		  cout << "func(char,char)" << endl;
	  }
	  
	  But if you write this,Will report an error,Because from the aspect of implicit transformation,There will be a choice
	  void func(int a,int b){
		  cout << "func(int a,int b)" << endl;
	  }

	  /*
	  void func(const int a){
		  cout << "func(const int)" << endl;
	  }
	  */	  //This is wrong. Frequent attribute overloading is required
	 
	 struct Stu{
		 int a;
	 }
	 
	 //Quote&
	 void func(Stu& s){
		 cout << "func(Stu)" << endl;
	 }
	 
	 void func(const Stu& s){
		cout << "func(const Stu)" << endl;
	 }
	 
	 void func(int *pi){}
	 
	 void func(const int *pi){}
	 
	 
	int main(){
		func();
		func('a');//If there is no char, it is automatically promoted to int
		func(108);
		func(97,'b');//Even if there is no error, there is implicit conversion
		
		return 0;
	}

2. Support default value (default value)

	long long int
	When defining a function,You can give function parameters default values(Equivalent to assignment statement),Then when you call this function
		(1)You can pass parameters for this location,It can also not be passed(Use default values)
		(2)If a parameter of a function has a default value,Then all parameters following this parameter must have default values void func(int x=1,int y)
			The default value is right
		(3)If the declaration and definition of the function are separated,Then the default parameter can only be placed in the declaration.h Can default,.cpp No
		(4))Pay attention to the ambiguity between overloading and not running,Running will be wrong
#include <iostream>
using namespace std;

//The parameter y has a default value. It can be passed or not passed. The default value is used 
long long int  pow(int x,int y=2){
	long long int res = 1;
	int i;
	for(i=1;i<=y;i++){
		res *= x;	
	}
	return res;
}

//	And the default value
long long int pow(int x){
		
}

/*
void func(int x=1,int y){//If func(2) is allowed
		
}
*/
//statement
void func(int x,int y=1);

void bar(int x,int y=1,int z=2,int w=3){
	cout << x << "," << y << "," << z << "," << w << endl;	
}

int main(){
	//cout << pow(10) << endl;// Call ambiguity
	cout << pow(10,2) << endl;
	cout << pow(10,3) << endl;
	//bar();
	bar(0);//0,1,2,3
	bar(100,200);//100,200,2,3
	bar(100,200,300);
	return 0;	
}

//void func(int x,int y=1) {/ / when separated, the default value can only be placed in the declaration
void func(int x,int y){

}

3. Support dummy yuan

	The arguments to a function have only types,Parameter without name,It is called dumb yuan
	In overload++,--Operators use dummy elements
	Note that overloading produces redefinition
#include <iostream>
using namespace std;

void func(void){
	
}

void func(int){//int 
	cout << "func(int)" << endl;	
}
/*
void func(int x){
	
}
*/
int main(){
	func(1);
	func(2);
	return 0;	
}

4. Support inline

	C++  inline Declared function
	(1)When calling an inline function, replace the binary instruction code of the function with the function call instruction,Reduce the time overhead of function calls
		This strategy is called inline
	(2)inline keyword,It is only recommended that the compiler compile the specified function inline,But it's just advice
	(3)Frequently called simple functions are suitable for inlining,Complex functions with few calls are suitable for inlining
	(4)Recursive functions cannot be inlined

5. Others

C Language has implicit declarations of functions,C++No,  C++Functions that are called must be declared first.
	C++If the parameter of the declared function is null(Nothing),On behalf C In language void Role of
	C++Arguments cannot be passed when a parameterless function is called in

C + + dynamic memory

C Dynamic memory:  malloc/calloc/realloc/free    sbrk/brk  mmap/munmap  
C++Dynamic memory:  new/delete  The underlying call is C Functions of language

new/delete Memory request for a single variable/release
 data type* Pointer variable = new data type;    //Auto initialize to "zero"
data type* Pointer variable = new data type(Initial value);
delete Pointer variable;

new[]/delete[] Used to apply for memory for arrays/release
#include <iostream>
using namespace std;

int main(){
	int *p = new int;//new a memory space of type int
	cout << *p << endl;//Here you will find that the address from new is the same
	*p = 1024;
	cout << p << endl;
	cout << *p << endl;

	delete p;//Free dynamic memory

	double *pd = new double;
	cout << *pd << endl;0 0 before assignment
	*pd = 3.14159;
	cout << pd << endl;
	cout << *pd << endl;
	delete pd;
	
	p = new int(1024);//This form of new has direct value
	cout << p << endl;
	cout << *p << endl;
	delete p;

	pd = new double(6.666);
	cout << pd << endl;
	cout << *pd << endl;

	return 0;	
}

new array

#include <iostream>
using namespace std;


int main(){
	int *p = new int[10]{111,222,1024};//Apply for 10 memory addresses of type int
	for(int i=0;i<10;i++){
		cout << p[i] << " ";	
	}
	cout << endl;
	
	for(int i=0;i<10;i++){
		p[i] = i;	
	}
	for(int i=0;i<10;i++){
		cout << p[i] << " ";	
	}
	cout << endl;

	delete[] p;

	return 0;	
}

What is the difference between new/delete and malloc/free?

(1)new/delete yes C++Operator  malloc/free yes C Functions of language standard library
(2)new When applying for dynamic memory, you do not need to calculate the memory size yourself
   malloc Specifies the number of bytes to request dynamic memory
(3)new The constructor of the class is called   malloc can't
   new The requested dynamic memory will not have garbage values
   delete When dynamic memory is released, the destructor of the class is called, but free can't
(4)malloc Error return NULL And set errno  
   new Error throw exception
(5)malloc The return value type is void*  
   new The return value type is new Pointer to the data type of the
(6)new Array memory required new[]
   malloc You can apply for array memory directly

quote

Reference is alias
 data type& Variable name = Target object
 be careful:
	Reference variables must be initialized directly,Parameter list references are not initialized directly(Initialize on function call)
	Once the reference is initialized,The reference object cannot be changed(Cannot point to other objects)
		Operations on referenced objects,Directly affect its target object
#include <iostream>
using namespace std;
void swap(int *pa,int *pb){//Pointer usage is familiar in C
	int tmp = *pa;//pa = &a;
	*pa = *pb;
	*pb = tmp;
}
//quote
void exchange(int& m,int& n){// M and n are aliases of arguments a and b. operations m and n are actually operation arguments
	int tmp = m;
	m = n;
	n = tmp;
}

int main(){
	int a = 1024, b = 9527;
	cout << "a=" << a << ",b=" << b << endl;

	exchange(a,b);

	cout << "a=" << a << ",b=" << b << endl;

	int m = 1, n = 2;
	exchange(m,n);
	cout << m << "," << n << endl;

	return 0;	
}

#include <iostream>
using namespace std;


int main(){
	int a = 1024;
	cout << &a << endl;//0x61fe0c
	cout << a << endl;

	int& r = a;//Define reference variable r reference a

	cout << &r << endl;//0x61fe0c
	cout << r << endl;
	
	r = 9527;//To operate on r is to operate on a
	cout << r << endl;//9527
	cout << a << endl;//9527

	a = 1212;
	cout << a << endl;
	cout << r << endl;
	

	double d = 3.14;
	double& rd = d;

	return 0;	
}

Reference variables must be initialized

	int main{
		! int &r;//Cannot compile
		int a=10;
		int& r1=a,r2;//r1 can be int & R2 can be int
		return 0;
	}

References cannot be constants directly

	!int&r =10;//Constants cannot be referenced
	const int&r =10;//That's it

Referenced application

	(1)The parameter of the function modifies the value of the argument in the function memory
	(2)As the return value of the function, you need to ensure that after the function call,The returned object is still valid
		Can return references to global variables, static variables, and dynamic memory objects(Just make sure it still exists after the function call)
		Cannot return a reference to a local variable
		double& getd(void){
			double d;
			return d;
		}//This may not work
		double& rd = getd();//Cannot return the application of a local variable
	
		The ability to operate depends on the value of the object referenced by the run compilation warning. It is uncertain,Behavioral uncertainty,May cause subsequent problems
	(3)Polymorphic parent class reference type references child class objects
#include <iostream>
using namespace std;

void func(int& ra){
	cout << ra << endl;
	ra = 1111;
}

!
/*
double& getd(void){
	double d = 6.666;
	return d;//Cannot return a reference to a local variable
}//Will you report this wrong
*/

int main(){
	//int& r;
	int a = 10;
	int& r1 = a,r2;//r1 is the reference int &, R2 is the int

	func(a);
	cout << a << endl;
	//double& rd = getd();
	//cout << rd << endl;
	return 0;	
}

References and pointers

	(1)The essence of a reference is actually a pointer,C++An encapsulation of a pointer,In many cases, references and pointers can be interchanged
	(2)C++At the level, references and pointers are different in the following aspects

A. The pointer is an entity variable, but the reference is not an entity variable

			sizeof(Pointer) == 4/8  When the platform and compiler determine,Unique size
			int a=2; int& ra=a;sizeof(ra)==4
			double d=1.0;double& rd=d;sizeof(rd)==8
			The size of the reference is determined by the type of object being referenced
#include <iostream>
using namespace std;


int main(){
	cout << sizeof(void*) << endl;//4/8
	cout << sizeof(int*) << endl;//4/8
	cout << sizeof(double*) << endl;//4/8
	
	cout << "------------" << endl;

	cout << sizeof(int&) << endl;//4
	cout << sizeof(double&) << endl;//8
	//cout << sizeof(void&) << endl;
	return 0;
}

B. Pointers may not be initialized, but references must be initialized

			int *p;//OK
			int& r;//Error

C. The pointer can modify the target (pointing to different addresses), but the reference cannot modify the reference direction

			int *p=&a;//Point to a
			p = &b;//Point b
			However, reference:
			int& r = a;//Citation a
			r=b;//The assignment of a becomes

D. You can declare a void pointer void * but you cannot declare a void reference void&

			void* p;//sure
			void& r=v;//error

E. You can declare a pointer to a pointer (secondary pointer), but you cannot define a reference to a reference

			int **p;
			int a; int&r = a;
			! int&& rra=r;//The referenced reference does not work
			int& rra =r;//Is that OK, or a

F. You can define a reference to a pointer, but you cannot define a pointer to a reference

			int *p;
			int*&rp = p;//References to pointers can
			int &r=a
			! int& *pr=&r;//The referenced pointer is not allowed

G. You can define a reference to an array, but you cannot define a referenced array

			The essence of array reference is that it refers to an array
			int arr[5]={1,2,3,1023,1024};
			//Pointer to array int (*arr)[5]
			int (*par)[5] = &arr;
			//quote
			int (&rar)[5] = arr;
			cout << arr << endl;
			cout << rar << endl;//Same address
			
			cout << rar[0] << endl;
	
			!//Referenced array
			int a,b,c,d,e;
			int& brr[5]={a,b,c,d,e};//error

C + + type conversion

Implicit type conversion automatic type conversion
	C++In, int* ---->  void*  Implicit type conversion
	       void* --->  int*  Implicit type conversion is not allowed
	Implicit type conversion can be performed between basic data types
 Cast type  (int)3.14
	(Target type)(Source object)

C + + provides four display type conversion operators

1. Static type conversion (which can be implicitly converted)

	static_cast<Target type>(Source object)
	If the target type and the source object type can be implicitly typed in one direction,
	Then static type conversion can be performed in both directions;
	Conversely, if implicit type conversion cannot be performed in both directions,
	Static type conversion cannot be performed in either direction
	
	Class types define custom conversion rules, or you can use static type conversion
#include <iostream>
using namespace std;


int main(){
	int x = 111;
	double d = 3.14;

	double ox = static_cast<double>(x);

	int od = static_cast<int>(d);

	void *p = &x;//int* --->  void*
	//int *pi = p;//void * cannot be implicitly converted to int*
	int *pi = static_cast<int*>(p);
	double *pd = &d;
	//pi = pd;
	//pd = pi;
	//pd = static_cast<double*>(pi);// Int * --- double * cannot perform implicit type conversion in both directions, so static cannot be used_ cast
	return 0;	
}

2. Type conversion of de constant attribute

	const_cast<Target type>(Source object)
	Only pointers (member pointers) and constant attributes of references can be removed
		C In language, local variables are const Modifier, indicating that it is read-only. The variable can be modified through a pointer
		C++In, local variables are used const If the variable is a constant, it will be directly replaced with this value during compilation. First, let's C++Medium const Make a verification of the feature
#include <iostream>
using namespace std;


int main(){
	const int x = 1024;//C + + optimizes const
	int *p = (int*)&x;
	*p = 9527;

	cout << x << endl;//1024 still hasn't been modified. The C language has been modified in theory, but C + + can't
	
	cout << *p << endl;//9527
	
	cout << &x << endl;//0x61fe14
	cout << p << endl;//0x61fe14 although the address is the same, it has not been modified
	
	cout << *&x << endl;//9527. You can get 9527 in this way, but the x value has not changed
	return 0;	
}

Examples of de constant attributes

#include <iostream>
using namespace std;

int main(){
	const int x = 10;
	//int y = const_ cast<int>(x);// It is neither a pointer nor a reference. This will report an error, because the exception can only be used for references or pointers
	const int& r = 10;

	//int& ri = x;// No, the const attribute is missing
	int& ri = const_cast<int&>(x);// Const int & constant attribute of the reference
	ri = 1111;	

	cout << x << endl;
	cout << ri << endl;


	int *p = const_cast<int*>(&x); //Remove const attribute of pointer

	return 0;	
}

3. Dynamic type conversion

	dynamic_cast<Target type>(Source object)
	Used between polymorphic parent-child class pointers or references

4. Reinterpret type conversion

	reinterpret_cast<Target type>(Source object)
	Used for type conversion between pointers or between pointers and integers
#include <iostream>
using namespace std;

int main(){
	int x = 10;
	double d = 3.14;

	int *pi = reinterpret_cast<int*>(&d);
	double *pd = reinterpret_cast<double*>(&x);

	pi = reinterpret_cast<int*>(x);
	/*
The code is obviously wrong because variable a, in essence, cannot be converted to a pointer type.
However, no errors will be reported during compilation. reinterpret_cast is the most unsafe type conversion function because the compiler does not check whether the parameter type is reasonable during conversion
	*/

	return 0;
}

C + + pointer type conversion

	1.Pointers can be implicitly converted to void* 
	2.void * Can use static_cast A pointer that converts a static type to another type  
	3.Different types of pointers can be used reinterpret_cast Reinterpretation type conversion
	4.Cast type
	5.Polymorphic parent-child type pointers can be used dynamic_cast Dynamic type conversion

Suggestions of the father of C + +

1.Use fewer macros and more const,enum and inline
2.Variables are initialized with the declaration
3.Use less malloc/free,Multipurpose new/delete
4.Use less forced type conversion. It is recommended to use display type conversion instead
5.Use less C Style string, multipurpose string
6.Establish the idea of object-oriented programming

Keywords: C++

Added by hewzett on Mon, 06 Sep 2021 06:40:31 +0300