# # # introduction

The content of learning is not the most difficult, the most difficult is whether to stick to it.

###C++ type conversion

C type conversion: parentheses are used to indicate the type of conversion, or not, for example, integer to floating point.

C++ type conversion

  1. Static_cast Common.
  2. Const_cast de-constant.
  3. Dynamic_cast Subclass type and superclass type intercast.
  4. Reinterpret_cast function pointer transitions, not portable (not commonly used).

Function:

  1. Improve code readability.
  2. Reduce existing risks.

###static_cast

A keyword widely used for type conversion.

Void * fun1(int type){switch (type){case 1: {int I = 9; return &i; } case 2: { int a = 'X'; return &a; } default:{ return NULL; Int * p_i = (int*)fun1(1); int* p_i = (int*)fun1(1); int* p_i = (int*)fun1(1); Char * p_c = static_cast (fun1(2)); system("pause"); }Copy the code

###const_cast removes constants

Int * p_i = (int*)&i; void fun2(const int I){// const int I = (int*)&i; *p_i = 20; / / c + + written, readability better int * p_i_1 = const_cast > < int * (& I); *p_i_1 = 30; }Copy the code

###dynamic_cast

Conversions of parent and child classes:

Class Person{public: virtual void print(){cout << "Person" << endl; }}; Class Man: public Person{public: void print(){cout << "Man" << endl; } void chasing(){cout << "chasing" << endl; }}; Class Woman: public Person{public: void print(){cout << "Woman" << endl; } void carebaby(){cout << endl; }}; //Woman* w = (Woman*)&p; //Woman* w = (Woman*)&p; //Woman* w = (Woman*)&p; //Woman* w = (Woman*)&p; //w->carebaby(); Woman* w = dynamic_cast<Woman*>(&p); if (w ! = NULL){ w->carebaby(); }}Copy the code

###reinterpret_cast

Function pointer conversion, you can understand.

void func1(){ cout << "func1" << endl; } char* func2(){ cout << "func2" << endl; return "abc"; } // Define the function pointer type typedef void(*f_p)(); Void main(){// function pointer array f_p f_array[6]; F_array [0] = func1; F_array [1] = (f_p)(func2); //C++ mode f_array[1] = reinterpret_cast<f_p>(func2); f_array[1](); system("pause"); }Copy the code

# # #, c + + IO streams

#### Text file operation

#include <fstream> void main(){char* f_name = "D://test.txt"; ofstream f_out(f_name); If (f_out.bad()){return; } f_out << endl; F_out << "I love you" << endl; f_out.close(); // Read the binary file ifstream f_IN (f_name); if (f_in.bad()){ return; } char ch; while (f_in.get(ch)){ cout << ch; } f_in.close(); system("pause"); }Copy the code

#### Binary file operation

void main(){ char* src = "c://src.jpg"; char* dest = "c://dest.jpg"; // Enter the stream ifstream fin(SRC, ios::binary); Ofstream fout(dest, ios::binary); if (fin.bad() || fout.bad()){ return; } while (! Fin.eof ()){// Read char buff[1024] = {0}; fin.read(buff,1024); / / write fout. Write (buff, 1024); } // Close fin.close(); fout.close(); system("pause"); }Copy the code

####C++ object persistence

Class Person {public: Person() {} Person(char * name, int age) {this->name = name; this->age = age; } void print() { cout << name << "," << age << endl; } private: char * name; int age; }; Void main() {Person p1(" Person ", 22); Person p2("rose", 18); Ofstream fout("c://c_obj.data", ios::binary); fout.write((char*)(&p1), sizeof(Person)); Fout.write ((char*)(&p2), sizeof(Person)); fout.close(); Ifstream fin("c://c_obj.data", ios::binary); Person tmp; fin.read((char*)(&tmp), sizeof(Person)); tmp.print(); fin.read((char*)(&tmp), sizeof(Person)); tmp.print(); system("pause"); }Copy the code

###STL

# # # # c + + string

STL: Standard Template Library, equivalent to the Util tool set in Java.

Note that the C++ string file is used here, not string.h.

#include <string> void main(){#include <string> void main(){#include <string> void main(){ string s2 = "s2"; cout << s1 << endl; //C++ uses strings similar to Java, and provides a series of methods: string s3 = s1 + s2; cout << s3.at(1) << endl; Char * c_str = const_cast<char*>(s3.c_str())); system("pause"); }Copy the code

# # # # c + + containers

Containers include maps, vectors, and so on.

Example:

#include <vector> void main(){#include <vector> void main(){ v.push_back(12); v.push_back(10); v.push_back(5); for (int i = 0; i < v.size(); i++) { cout << v[i] << endl; } system("pause"); }Copy the code

If you feel that my words are helpful to you, welcome to pay attention to my public number:

My group welcomes everyone to come in and discuss all kinds of technical and non-technical topics. If you are interested, please add my wechat huannan88 and I will take you into our group.