C ++ reading notes

        leetcode brush it more than 600 questions, questions the recent slowdown in brush speed (specifically, the game is played only weeks), began to see the C ++ related content.

        The content will be continuously updated nearly in a short time, but in itself lacks technical content, because it is just my personal notes.

        Content: C ++ Primer / Effective C ++

Operator overloading

        Copy assignment operator, C-style conversion assignment operator:

String& operator = (const String& );
String& operator = (const char* );

        append operation:

String& operator+=(const String& );
String& operator+=(const char* );

        As the operator overloading class members break a problem that does not support the following statements:

String flower;
if(flower == "lily") // ok
else if("tulip" == flower) // error

       Here, we hope that the presence of an operator to meet the left operand is a C-style strings, the right operand is of type String string, but does not actually exist, the compiler will not do implicit conversions. (See all required class definitions, can be found for the left operand conversion type constructors, inefficient)

       As a non-member function of writing:

bool operator == (const String&, const String& );
bool operator == (const String&, const char* );

        After the original intention is to provide a heavy duty: taking into account the overhead of C-style strings to the String type conversion.

        The following must be a class member operators:

=, [], (), ->

Survival

        When you start the program allocation, destroyed at the end of the program: the global object.

        Local native objects: Create entering the defined block, leaving destruction of blocks.

        Local static objects: for the first time before the distribution, the destruction of the end of the program.

        Dynamically allocated objects: displays destroyed when released. (Free store / heap)

shared_ptr class

shared_ptr<string> p1;
shared_ptr<list<int>> p2;

        The default initialization smart pointer holds a null pointer.

RTTI

         Runtime type identification: a pointer to the base class / type of the actually acquired reference.

        (1) dynamic_cast operator, security type conversion.

        (2) typeid operator can get the actual derived type.

        More use should compile-time type checking.

Guess you like

Origin www.cnblogs.com/fish1996/p/11494070.html
Recommended