1 background

Read the ART source code for the purpose of learning.

2 Data Types

2.1 Basic data types

Because C++ is more closely associated with hardware platform features, the number of bytes required for each type is not as strict as Java. Instead, it specifies the minimum number of bytes required for each type. For floating-point types, the least significant number of digits is specified.

The sizeof operator is used to get the number of bytes for each type.

  • Short: A short integer of at least 2 bytes
  • Char: contains at least one byte
  • Int: An integer of at least 2 bytes, usually 4 bytes
  • Long: a long integer of at least 4 bytes
  • Long long: A long integer of at least 8 bytes
  • Float: single-precision floating-point type that supports at least six significant digits
  • Double: supports at least 10 significant digits
  • Bool: Boolean type, not defined by specification

2.2 Pointers, references, and void

Represents the pointer type T *, where T represents some data type

Represents the reference type T &, where T represents some data type

Void denotes the null type void *, which can only be used to modify pointer variables. Indicates that you do not care about the data type in memory to which the pointer points

2.2.1 pointer

  1. Type of pointer
  2. Assignment of a pointer
  3. Dereference of a pointer

1. Type of pointer

The pointer essentially represents the address of virtual memory, i.e., the pointer is the address of memory. For example, on a 32-bit operating system, if a process has a virtual address space of 4 gb, the virtual address ranges from 0x0 to 0xFFFFFFFF(8 fs), and any of these values are addresses, or Pointers.

What does a running program have in its virtual memory? It must be data or code.

If memory stores data, since data is typed, Pointers to that memory are also typed. Int * p: p is a pointer to the address of the memory in which an int is stored.

If memory stores code, typically an entry to a function, a pointer to that address is called a function pointer.

Int mymain (int argc, char* argv[]) {} int mymain (int argc, char* argv[]) {} int mymain (int argc, char* argv[]) {} Char * argv[]) // Pmain =mymain; pmain=&mymain; // Call pmain(0,nullptr);Copy the code

2. Pointer assignment

  1. Write an address directly to the pointer
  2. The new operator allocates a block of memory on the heap, the address of which is stored in the corresponding pointer variable
  3. Retrieves the address of a variable or function by taking the address symbol &
int* p=(int*)0xff43kk99; This operation is dangerous!! Obj* obj=new Obj(); int a=100; int* pa=&a; int (*pmain)(int a,char* b[]); pmain=mymain; pmain=&mymain;Copy the code

3. Pointer dereference

Pointers are memory addresses, so how do you get the contents of memory? By *.

For data type Pointers, * means to get data in memory. For function Pointers, * means to call a function

Int * a = 100; // The value *a=100; // Call function (*pmain) (100, nullptr);Copy the code

2.2.2 reference

References are aliases for variables. There is a strong one-to-one relationship between the reference and the original variable. A reference and a variable are the same thing. It is the relationship between Zhou Shuren and Lu Xun.

2.3 Characters and Strings

2.4 an array

Pointers and arrays have a natural relationship.

int *parray=new int[4]; Parray + I: indicates the address of subscript I. * (prray+ I) : indicates the value in memory with subscript ICopy the code

3 Source code composition and compilation

The files that host source code in C++ are divided into header files and source files. The extension names of header files end with. H,. HPP, and HXX. The file name extension ends with. Cc,. CPP, or CXX.

Header files are used to publicly declare variables, functions, and classes.

The source file is an implementation of the header file that implements these variables, functions, or classes.

Namespace: Java-like package that represents a scope scope. Variables, functions, or classes declared in a namespace belong to this namespace.

Test1 is defined in the namespace of the header file:

//type.h

namespace my_type{
    void test1();

}

Copy the code

Test1 and test2 functions are defined in the namespace of the source file:

#include "type.h" namespace my_type{void test1(){void test2(){// void test2()}Copy the code

The test2 function is unknown to users (who generally only know include headers).

#include "type.h" int main(void){my_type::test1(); }Copy the code

The C++ standard library STD is a namespace.

4 Class related

The overall understanding of the class class comes from three aspects:

  1. Class is the core unit of the object-oriented world, which encapsulates member variables and member methods. Developers are more concerned with designing sensible classes and dealing with the relationships between objects. The focus is on the interaction between objects.
  2. Classes support abstraction, inheritance, and polymorphism. The focus is on classes and relationships between classes.
  3. From a data type perspective, a class is also a data type, but a developer’s custom data type.

A complete class example: typeclass.h

#ifndef _TYPE_CLASS_H_ #define _TYPE_CLASS_H_ namespace type_class{ void test(); class Base{ public: Base(); // Base(int a); // Base(Base & other); // Copy construct Base& other =(Base& other); // Copy the assignment function Base(Base && other); // Move the constructor Base&& other =(Base&& other) // move the assignment function ~Base(); // Destructor protected: // declare and implement int getMemberB(){return memberB; } int deleteC(int a, int b=100,test=true); Private: int memberA; int memberB; int* membeC; }}Copy the code

4.1 Construction, assignment, and destructor

The constructor is used to initialize objects, including member variables. Constructor class:

  • The default construct has no arguments
  • Normal construction: initialization with parameters
  • Copy construction: this is deep copy, that is, when the data of reference type is encountered, the memory will be reallocated and the original memory will be copied.
  • Move construction: Move the value of object A directly to object B, and object A is destructed.

Assignment: Copy assignment: the copy process is performed. Move assignment: only moves, improving efficiency

Destructor: The destructor is called back when an object is reclaimed. An object is created on the stack that executes a destructor before the function returns. An object is created by dynamic new and the destructor is called back when the object is deleted.

4.2 Derivation and inheritance of classes

4.2.1 Virtual functions, pure virtual functions, and virtual destructors

This parameter is decorated by the virtual keyword.

Virtual void test (); // Virtual void test2()=0; // A pure virtual functionCopy the code

Virtual functions: Derived classes can override this method. When overridden, subclasses are called. Pure virtual functions: Derived classes cannot be overridden. Similar to interface virtual destructors: to be polymorphic, the destructor in the base class must be dashed.

Call order? The constructor is called at instance creation time. Base classes precede derived classes. Destructors do the opposite. Class Drive: Base, VirtualBase{}

Constructor order: Base in VirtualBase and last in Drive.

Destructor order: First Drive in VirtualBase in Base.

Friends and forward declarations for class 4.3

The concept of friends: provides a way for functions or classes outside a class to access a class’s private member variables or functions. For the class being accessed, these classes are the friends of the class being accessed.

Friends are not inherited by friends.

friend void accessObj(); Friend void B::testa ();Copy the code

Classes can be declared first and defined later.

Explicit is used to avoid type implicit conversions.

Structs are almost the same as classes, but the members of structs are public.

5 Operator overloading

The essence of operator overloading is to treat operators as functions. When an operator is called, it is equivalent to calling a function. Definition:

Operate Indicates the symbol of the operatorCopy the code

The -> and * operators are overloaded.

Intelligent pointer can be realized. The effect is similar to that of a pointer by overloading time.

But for real Pointers, -> is equivalent to accessing a member variable or function. * is dereference.

6 Function templates and class templates

From a data type point of view, that long abstraction of a T. It can represent either an int or a long.

A template can be instantiated using concrete data types.

  • Abstraction of data types
  • instantiation

6.1 Function Templates

The reason: Because the compiler needs to know all the types when instantiating a template.

Function templates are not functions by nature, and the actual function is generated only when compiled to apply the specific type to the template function.

// Template <typename T1,typenae T2,typename T3=long> T3 add(T1 a,T2 b){return a+b; } template<typename T, (*compare) (T& a,T& b) > void add22(T& a,T& b){return a+b; }Copy the code

Function template specialisation:

The special case is the actual function. Not a template function.

Format: Template <> modifier that directly replaces a type in a modulated function with a specific type.

6.2 class template

Format:

template<typenmae T> Class TObj{ template<typename T1,typenae T2,typename T3=long> T3 add(T1 a,T2 b){ return a+b; }}Copy the code

A class template can have either ordinary functions or template functions (also known as member templates).

7 Lambda expressions

Lambda expressions in C++ are essentially anonymous function objects. Format:

Auto f =[capture list] (function name) -> return type {function body} auto func (int a) ->int {... }Copy the code

Creating a lambda expression results in a closure, which is an anonymous function object.

8 the STL introduction

Standard template library. The namespace is STD.

8.1 the string class

8.2 container class

Dynamic array Vector: Analogies to Java’s ArrayList

Hash table unordered_map —-hashMap

List the list – linkedList