C + + Concept summary (based on c++11)

Source: Internet
Author: User

C + + Concept summary (based on c++11)

Structure
  • Constructor form:
    • Default constructor
    • Copy constructor
      • Definition: When a new object is defined and initialized with an object of the same type, the copy constructor is used explicitly
      • Form: A (const a& h) {}
      • Call Time:
        • When an object is passed as a parameter
    • Assignment operators:
      • Definition: Assignment operators can be overloaded by making different types of right-hand operands.
      • form: a& operator = (const a& h) {}
      • Call Time:
        • When an object needs to be assigned with ' = '
    • Move constructor
      • Definition: Control of Source object resource is given to target object
      • Form: A (a && h) {}
      • Call timing: Assigning using temporary objects
    • Move assignment operator: basic ibid.
  • In C++11, the constructor is inherited by the quilt class and is implicit.
  • Delegate constructs: Delegate constructors delegate construction tasks to target constructors
    • Delegate constructors cannot have an initialization list
  • More variety of initialization forms in C + +
    • equal sign "="
    • Equal sign plus curly brace int a = {3 + 4}
    • parenthesis expression int A (3+4)
    • Curly brace Initialization list int a{3+4}
      • For custom classes, you need to use the Initializer_list constructor
Move semantics
    • Value types in C + + are classified as Lvalue, rvalue, and dead values (which can be simply referred to as rvalue values)
    • Rvalue Operation: Std::move/std::forword
    • Reference folding: (&..) can always be folded to the right &/&&)
POD type

POD (Plain old data) is literally understood, is the original data, it embodies the compatibility with C, its advantages are as follows

    • byte assignment, in which we can safely use memset/memcpy to initialize and copy POD types.
    • Provides compatibility with C memory layouts. C + + programs can operate with each other, because POD-type data is always safe for operations between C and C + +
    • The static initialization is guaranteed to be safe and effective. Static initialization can improve the performance of the program in many cases, and the POD type Object initialization is often simpler (such as the. BSS segment that is placed in the target file, which is directly assigned 0 in the initialization).

Must be trival or standard layout to satisfy the POD type

    • Trilvial
      • Constructor/destructor/copy construction/move construct/Copy assignment operator must be the default
      • Cannot contain virtual functions and virtual base classes
    • Standard Layout
      • All non-static data members conform to the standard layout type, and their base classes conform to the standard layout.
      • All non-static members have the same access rights
      • There are no virtual functions and dummy base classes
      • The type of the first non-static member in a class differs from its base class//Because an object of the same type defined in the standard for C + + must have a different address!
      • When a class or struct inherits, one of the following two conditions is met
        • There are non-static members in the derived class, and there is only one base class that contains only static members.
        • The base class has non-static members, and derived classes do not have non-static members.
          (In other words, non-static members cannot exist in both the base class and the derived class?)
C++11 introduction of non-restricted consortium support custom literal inline namespaces
    • Can be found directly in the parent scope
    • ADL: Can be pushed directly through the object's namespace
The Using extension
    • can replace typedef
    • More flexible usage of the template using mapstring = Std::map
Dynamic type
    • Auto
    • Information about the classes that can be queried through typeid
    • Decltype push to four rule (assuming E is type T)
      • If there is no parenthesized token expression or class member access expression, the push-to-type T = Decltype (e)//Expression is returned
      • Otherwise, if E is a dead value, then return t&&
      • Otherwise, if E is an lvalue, it returns t&
      • Otherwise, the push-to value of Decltype (e) is T//object E
C++11 introducing enum class: Enum class type{} smart pointer

The smart pointer is actually a stack object, not a pointer type, and at the end of the life of the stack object, the smart pointer frees up the heap memory it manages through a destructor

    • Std::auto_ptr manages objects obtained from the new form and releases the object when the auto_ptr is destroyed. Copying this pointer object will cause the object it holds to transfer ownership.
    • STD::SCOPED_PTR exclusive ownership, non-reproduction
    • Std::shared_ptr comes with reference counting and looks perfect
    • Std::scoped_array array pointers, which are also disabled for copy copying
    • Std::shared_array array pointers, no longer disables copy replication
    • Std::weak_ptr references, but not counting
    • Std::intrusive_ptr Insert smart Pointer, need to manually increase the count
Type conversions
    • Implicit conversions
      • Arithmetic conversions (arithmetic conversion): In a mixed-type arithmetic expression, the widest data type becomes the target conversion type.
      • One type of expression is assigned to another type of object: The target type is the type of the object being assigned (when the void pointer is assigned to another specified type pointer, there is no standard conversion, compilation error)
      • Passing an expression as an argument to a function call when the parameter and argument types are inconsistent: the target conversion type is the type of the parameter
      • Returns an expression from a function that is inconsistent with the return type: The target conversion type is the return type of the function
    • An explicit type conversion
      • Static_cast < Type-id > (expression)
        • Basic type conversions, but there is no run-time type check to guarantee the security of the conversion.
        • Static_cast cannot convert a const, Volitale, __unaligned attribute.
      • dynamic_cast < Type-id > (expression)
        • Description: Type-id must be a pointer to a class, a reference to a class, or a void *;type-id/expression must be the same as a pointer or reference.
        • Dynamic_cast features that have type checking
      • Reinpreter_cast (expression)
        • Keyword Re-inpreter re-interpretation! Mutual transfer between types of non-pass
      • Const_cast (expression)
        • Go to the constant nature
Lambda expression

[Capture] (params) mutable exception attribute, ret {body}

    • [] do not intercept any variables
    • [&} intercepts all variables in the outer scope and uses them as references in the body of the function
    • [=] intercepts all variables in the outer scope and copies a copy to be used in the body of the function
    • [=, &foo] Intercepts all variables in the outer scope and copies a copy to be used in the function body, but uses a reference to the Foo variable
    • [bar] Intercept bar variable and copy one copy at function weight without intercepting other variables
    • [x, &y] x is passed by value, Y is passed by reference
    • [This] intercepts the this pointer in the current class. This option is added by default if you have already used & or =.
Constant-expression constexpr
    • Constant-expression functions:
      • The function body has only a single return
      • Must have a return value
      • must be defined before use
      • Returns a function in a statement expression that cannot use a very literal expression, global data, and must be a constant expression
    • For custom type constants, you need to add a constant constructor constexpr T () {}
    • C++11 support variable length template//representative = = Tuple
    • constexpr for compile-time determination
Atomic action Template: std::atomic Alignment support: alignof/alignasquick_exit/at_quick_exit

C + + Concept summary (based on c++11)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.