Basic C + +

Source: Internet
Author: User
Tags assert try catch

When we define a class, if it is an empty class, the compiler defaults to providing the default constructor, copy constructor, copy assignment function, and introverted destructor.

If you derive a new class that inherits from the empty class, the compiler generates the same, but the constructor of the base class is called when it is constructed.

has written a partial class function, the compiler will give you the rest of the function default generation.

If you have your own constructor, the compiler generates additional constructors, but does not generate a default constructor.

If you do not write return 0 at the end, exit (0) will be called at the end of the default, so it is also possible not to write return 0.

C + + itself has an exception, not necessarily at the end of the return.

The introduction of Const invariance, multithread is inseparable. was first introduced in C + +.

Constexpr after the compiler gets enough information, the results can be obtained at compile time.

Decltype and Auto are very similar, one of the differences is that there is no assignment operation, only the type itself is used.

If the expression is prefixed with a parenthesis, the type is a reference to that type, and the reference initialization is required, otherwise an error is made.

Auto can also, however, specify the type of reference to display.

More often, Decltype is used with auto, and Auto is placed in front, and returns this form after using Decltype tail.

In the case of a dynamic_cast type conversion, if it is a pointer type, the conversion fails and throws null if the reference type throws an exception.

The function in class is inline by default, and inline is required when implemented outside.

(void*) 0 define in C language

Use of static

Inside the file usage

In the global scope of the file is visible, the program is compiled when the memory space has been allocated,

Defined within a function, equivalent to a function having a state.

Static the use of classes and structs in a class is only equivalent to declaring, and the definition of the class must be externally

This is the time to allocate memory space to find this variable.

Static_assert and assert are similar, but it is in the compiler has not yet to connect the process if not meet the requirements of the error.

Assert is actually running the process will not find the problem, assert is a macro, in debug and release below the behavior is not the same, under release will be automatically ignored.

Static_cast does not have any performance penalty, and tells the compiler to do this to force type conversions.

Thread_local C++11 introduces the keyword to facilitate multi-threading programming.

--------------------------------------------------------------------------------------------------------------- -

C++98 c++11

The predecessor declaration of a class is an incomplete type, void is also an incomplete type, and is simply a declaration of a type.

The benefit of the predecessor statement, why use the predecessor declaration.

As a base, a member of a class, a member of a class, these things cannot be omitted,

In other cases, such as a parameter interface or a function return value, you just need to know what is on the line, that is, the declaration is sufficient.

Class is the size of the class that is most needed in the compilation process.

Construction and destruction

C language Why does not have a process of destruction, the reason is not because the C-language control flow is clear

However, in the process of control of C + + program, it is possible to interrupt the process by throwing out an exception, and the resources to be controlled should be released at this time.

Java's resource release is ambiguous, and it may be that the system frees up resources at your busiest time. Also is the program manages the resources not only is the memory, also has the file handle, has already connected and so on. These are also required to be managed manually.

C + + has a destructor, the existence of when the release for the programmer is clear, there are two cases, one is the class automatic destruction when the new resource delete will be released.

3 Basic Principles in C + +

When you have a member variable of pointer type in a RULE1 class member, you must write a destructor to manage the resource. You cannot use the compiler default destructor, which defaults to invoking the destructor of the class member, and if the class is derived from another class, the destructor of its parent class is called.

Rule 2 Rule3: If a class can be copied, the copy constructor and copy assignment function need to be written out. If no write will be generated automatically, this will only copy the members of the class, and the default copy is the value copy.

If it is not necessary to c++98 the practice is to privete privatization, and is only a function, this really run time to make decisions.

The practice in c++11 is to use the =delete keyword, which is a matter of decision making at compile time.

What is an lvalue and what is a right-hand value? The right value is not the address, the left value can be taken address, this is the most obvious difference.

In C + +, a large number of copies of the object are done before, and the incoming outgoing of the function calls the copy constructor a lot.

In this case, the compiler can be optimized, known as optimize value.

Rvalue reference, a new concept introduced in c++11. C + + explicitly returns an Lvalue or returns an rvalue that can be distinguished.

Since it is possible to differentiate between the use of it, to reduce the corresponding copy is a big problem.

Std::move is to turn an lvalue into a right value. The right value is equivalent to a temporary variable, the temporary variable cannot be referenced, but the const reference can be taken. When a function parameter is a const reference, the function parameter can be lvalue, rvalue, temporary object, const reference, all can be, is simply the omnipotent parameter.

If there is no write move constructor, the default is to call the copy constructor, so the Rvalue reference is equivalent to a temporary variable, and the copy constructor parameter allows a temporary variable to be accepted, so the copy constructor is called by default.

Rule4 Rule5:

When a copy constructor passes in an rvalue, it now has the ability to differentiate whether the passed-in is an lvalue or an rvalue, which was previously not capable of distinguishing between an lvalue or an rvalue.

What is the copy constructor of the right value to do?

is to swap pointer variables and change the pointer variable of the right value to null.

Never throw an exception in a destructor

The compiler-generated destructor is inline by default and generates Noexcept. And will not do anything that does not include a try catch similar statement inside. The destructor of the member function is also called after the self-destruction is completed

38 sections

Basic C + +

Related Article

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.