C++ overview
1. Why choose C++ language for embedded development?
(1) The characteristics of procedural programming
C language features: C language is gradually improved in the process of practice
· No thoughtful design process
· There are many “gray areas” in use
…
· Characteristics of low-level languages with too many residues
· Direct use of Pointers for memory operations
…
Procedural programming features:
Procedural programming: Data structures + algorithms
· Mainly solve scientific computing problems with simple and fixed user needs
· Features: Analyze the steps needed to solve the problem
Use functions to implement each step
Call the function in turn to solve the problem
· Problem: Poor reusability of software
Software maintainability is poor
The software is not built to meet user needs
(2) The characteristics of object-oriented programming
Object-oriented programming features:
Object-oriented programming: Modeling software from the real world
· Directly map things in the real world to the program, which can directly meet the needs of users
· Features: Directly analyze the entities involved in user requirements
Describe real-world entities in code
Relate entities in code to work together to solve problems
· Advantages: Built software that can adapt to changing user needs
Take advantage of the advantages of a process-oriented approach and avoid its disadvantages
C++ features: efficient object-oriented language, and compatible with existing code
2. Why is C++ difficult to learn?
C++ supports programming formats:
Procedural,
· Data abstraction
· Object-based
· Object oriented
Functional,
· Generic form
· Template meta-form
Value semantics and object semantics:
Value semantics can be copied and assigned, but object semantics cannot
3.C++ related basic knowledge
Who is the father of C++?
Benjani Strausup
In 1982, Dr. Bjarne Stroustrup of AT&T Bell LABS introduced and expanded the concept of object oriented on the basis of C language and invented a new programming language. To reflect the language’s c ancestry, it was named C++. Dr. Bjarne Stroustrup is known as the father of C++.
(2) C++ language standards
C + + 98 standard
The first version of the C++ standard, released in 1998. The official name is ISO/IEC 14882:1998[17].
C + + 03 standards
The second edition of the C++ standard was released in 2003. The official name is ISO/IEC 14882:2003[18].
The standard c + + 11
C++ standard third edition, released on August 12, 2011. The official name is ISO/IEC 14882:2011[19].
C++11 makes three major changes to the methods of container classes.
First, the new rvalue reference makes it possible to provide movement semantics to the container. Second, because of the new template class initilizer_list,
Therefore, constructors and assignment operators that take initilizer_list as arguments have been added. Third, the addition of variadic templates and function parameter packs makes it possible to provide emplacement methods.
14 c + + standard
The fourth edition of the C++ standard was released on August 18, 2014. The official name is ISO/IEC 14882:2014[21].
C++14 is an incremental update to C++11. It mainly supports the return type deduction of ordinary functions, generic lambda, and extension
Lambda capture, revision of constEXPr function restrictions, ConstExpr variable tempetization, etc. [22].
(3) new C++11 features worth learning
· Smart Pointers such as shareD_PTR, Weak_PTR, etc
· rvalue reference
The function/bind
, a lambda expression and closure
Upgrade from C to C++
1. Declare definitions
C++ : in C++, there is more emphasis on the utility of the language. All variables can be defined as needed
For (int I = 0; i < 2; i++)
C: Variables must be defined at the beginning of the scope
2. Upgrade the register keyword
We can modify frequently accessed variables with register as register variables, and request the compiler to store variables in the INTERNAL register of THE CPU as much as possible, which saves the TIME for the CPU to grab data from the memory, thus improving the operation efficiency.
C language: Register can only modify local variables, not global variables and functions;
A register variable cannot get a register variable by fetching an address.
Register modifies variables that must be of a data type acceptable to the CPU.
C++ : register keyword is still supported in C++
The C++ compiler has its own optimizations that can be done without using register
C++ can fetch the address of the register variable (the C++ compiler discovers the need to fetch register
Register does not declare the address of a variable
3. The const keyword
C++ : the C++ compiler’s handling of const constants
· Place a constant in the symbol table when it encounters a constant declaration
· If a constant is found during compilation, replace it with a value from the symbol table
· If an extern or & operator is used for const during compilation, the corresponding constant is given
Allocating Storage Space
Note: while the C++ compiler may allocate space for const constants, it does not use its storage space
The value of the.
C: In C, const modifies a variable. The value of the space is mutable, but the corresponding value of the space cannot be modified by the name of the variable.
4. Allocate and release memory
(1) dynamic memory allocation in C++
· dynamic memory application through the new keyword in C++
·C++ dynamic memory requisition is based on type
· The delete keyword is used to free memory
Variable declaration:
Type *pointer = new Type;
…
delete pointer;
Array clear:
Type *pointer = new Type[];
…
delete[] pointer;
(2) Difference between new and malloc
· The new keyword is part of C++ and malloc is a function provided by the C library
· New allocates memory in units of specific type, while malloc allocates memory in bytes only
distribution
· New can be initialized when applying for a single type variable. Malloc has no memory initialization feature
sex
5. References vs. Pointers
(1) Citation
· A reference is an alias for a variable
· Define the general format of a reference:
· Type & reference name = variable name
· For example: int a = 1;
int &b = a; //b is an alias for A, so a and B are the same cell
Note: When defining a reference, be sure to initialize it to indicate whose alias the reference variable is
· In practical applications, references are generally used as parameter passing and return values
(2) CONST references are references to const objects
(3) Function parameters: pass by reference
The reference is passed by adding the operator ampersand to the parameter in the function definition.
For example, void swap(int &a,int &b);
· Passing by value is easy to understand, but the parameter is worth changing without affecting the argument
· Address passing changes the argument of the response by changing the parameter, but the program is error-prone and difficult to read
· Reference any operation on a parameter as an argument changes the data of the corresponding argument and makes the function call look convenient and natural.
(4) Function return value: reference as function return value
· Another function of a reference is a function that returns a reference
· One of the main purposes of a function return reference is to place the function to the left of the assignment operator
· Note: References to local variables cannot be returned
(5) The difference between reference and pointer
· Reference access to a variable is direct access, whereas Pointers are indirect access
· A reference is an alias for a variable and does not allocate its own memory space, whereas a pointer has its own memory space
· Once a reference is initialized, it cannot refer to any other variable, whereas a pointer can
· Use references whenever possible and Pointers when necessary
6. Function upgrade
(1) Inline function: space for time (called frequently and briefly)
Use of inline functions:
inline int max(int a,int b)
{
return a>b? a:b;
}
#define MAX(a,b) (a)>(b)? (a):(b)
The difference between inline functions and parameterized macros:
· When an inline function is called, arguments and parameters are required to be of the same type. In addition, the inline function will evaluate the real first
The parameter expression is evaluated and passed to the parameter; Macro calls, on the other hand, simply replace forms with arguments
The cords.
· Inline functions expand the code at compile time and at call time, while macros are preloaded
A substitution is made during processing
· in C++, inline functions are recommended to replace macros with arguments
(2) Function overload
For the same scope, if two functions have the same name but different parameters, we call them overload
Conditions for function overloading :(the return value of a function cannot be a condition)
· Different forms of function overloading:
· Different number of parameters
· Different parameter types
· Parameter order is different
· The number and type of parameters are different
· When an overloaded function is called, the compiler checks the number, type, and order of arguments to determine the response
The called function of
Name Managling and extern “C” :
· Name managling here translates to name adaptation
·C++ requires name managling in order to support overloading
· Extern “C” implements mixed C and C++ programming
#ifdef__cpluscplus
Extern “C”
{
#endif
…
#ifdef__cpluscplus
}
#endif
(3) Functions with default arguments:
· The default value of the parameter in the function definition when the function is not declared
· If a function is defined and declared, the default value cannot be specified after the declaration
· Default values must be defined from right to left, if there is no default value for a parameter
The arguments to the left cannot have default values
Void funcl(int a,double b = 4.5,int c = 3
Void funcl(int a = 1,double b,int c = 3) void funcl(int a = 1,double b,int c = 3
· When a function is called, the actual participation parameters are matched from left to right
7. Namespace – Namespace
(1) Namespace
· There is only one global scope in C
· All global identifiers in C share a scope
· There may be conflicts between identifiers
· the concept of namespace is proposed in C++
· Namespaces divide the global scope into different parts
· Identifiers in different namespaces can have the same name without conflict
· Namespaces can be nested with each other
· The global scope is also called the default namespace
(2) How to define a namespace
namespace First
{
int i = 0;
}
namespace Second
{
int i = 1;
}
using namespace First;
int main()
{
cout<<i<<endl;
cout<<Second::i<<endl;
return 0;
}
(3) How to use namespaces
· Using namespace name;
· Use variables in the namespace: using name::variable;
· Use the variable in the default namespace :: :variable
It can be used directly by default
All identifiers in the default namespace
8. New type conversion operators
(1) static_cast < T > (expr)
· Used for conversions between primitive types, but not between Pointers to primitive types
· Used for conversions between objects of classes with inherited relationships and between Pointers to classes
int main()
{
int i = 0;
Char c = ‘c’.
int *pi = &i;
char *pc = &c;
c = static_cast
(i); //success
(i); //success
pc = static_cast
(pi); //error
(pi); //error
return 0;
}
Static_cast is converted at compile time and cannot detect types at run time, so converting between class types can be risky
(2) const_cast < T > (expr)
Const_cast cast — used to remove const attributes of variables
int main()
{
const int &j = 1;
int & k = const_cast<int&>(j);
const int x = 2;
int & y = const_cast<int&>(x);
k = 5;
cout << j << endl;
cout << k << endl;
y = 3;
cout <<x<<endl;
cout<<y<<endl;
cout <<&x<<endl;
cout<<&y<<endl;
return 0;
}
(3) the reinterpret_cast < T > (expr)
Reinterpret_cast Casts
· Used for casting between pointer types
· Used for casting between integer and pointer types
typedef void(pf)(int)
int main()
{
int i = 0;
Char c = ‘c’.
int * pi = reinterpret_cast<int *>(&c);
char * pc = reinterpret_cast<char *>(&i);
PF * pf = reinterpret_cast<PF*>(0x12345678);
c = reinterpret_cast<char>(i);
return 0;
}
A reinterpret_cast performs an assignment directly from a binary, and is an extremely unsafe conversion.
(4) dynamic_cast < T > (expr)
Dynamic_cast Cast
· Mainly used for conversion between class hierarchies, and can also be used for cross-transfer between classes
· Dynamic_cast also has type checking, which is more secure than Static_cast