C + + Primer 5

Source: Internet
Author: User


User-defined identifiers cannot appear consecutively with two underscores, and cannot be underlined with the beginning of a capital letter. Additionally, identifiers that are defined outside the function body cannot begin with an underscore.

(Try to g++ and VC can be used normally, these are actually the compiler's own implementation.) ))


References are just aliases,

The reference itself is not an object, so reference references cannot be defined. (int &&rr = R;//error)

The reference itself is not an object, so a pointer to a reference cannot be defined.  (int &*p = &r; Error



Const char* p = "ABC";

Std::cout << cast_cast<const void*> (p); This allows the value of the pointer to be output, otherwise the default output points to the string.


By default, the const object is only valid within the file. If you want to share a const object between multiple files, you must

Add the extern keyword before the definition of the variable.

file1.cpp extern const int bufSize = FCN ();

File1.h extern int bufSize;


Pointers and constexpr (P60)

In a constexpr declaration, if a pointer is defined, the qualifier constexpr is valid only for pointers, regardless of the object that the pointer refers to.

const int *p = nullptr; P is a pointer to an integer constant

constexpr int *q = nullptr; Q is a constant pointer to an integer


If the content of an expression is a dereference operation, Decltype will get the reference type.

Decltype (*p) C; Error, C is int&, must be initialized.

The result of Decltype (&p) is int**;

Note: The result of Decltype ((v)) is always a reference, whereas the Decltype (v) result is only in V itself

A reference is a reference.


The header file should not contain a using; it may be copied to other files that reference it, and is prone to conflict.


Difference_type the distance between 2 iterators. can be positively negative.

Ptrdeiff_t\ size_t is defined in Cstddef. can be positive negative

The best way to understand the meaning of an array declaration is to start by reading the array name from inside out. (p103)


int *ip[4]; The array IP of an integer pointer is an array of 4 elements, with an int pointer on it

int (*IP) [4]; Point to an array of 4 integers (IP is a pointer) to an array of 4 elements, all of which are of type int


A constant object, as well as a reference or pointer to a constant object, can only call a constant member function.

Explicit, which can be used in the form of direct initialization.


The tie has 2 overloaded versions, 1 returns pointers to the output stream without parameters, and 2 accepts a ostream pointer that associates itself to this ostream.


container, if the element type does not have a default constructor, you must also make an explicit element initial value in addition to the size parameter


Std::array Specify the container size in addition to the element type.

Std::array<int, 42> A;

Std::array<int, 42>::size_type s;


Erase returns a pointer to the next element in the sequence.

Insert inserts a new element before the given position, and then returns an iterator that points to the newly inserted element.

For vector\string and so on container insert, delete all "easy" will cause the iterator invalidation!!

Do not save End ();


Fill_n (Back_inserter (Ivec), 10, 0);


Lambda

[Capture List] (param list), return type {function Body}

= value capture, & reference capture.


Ref (VAR); In the way of reference ...


p426: Unlike Unique_ptr, shared_ptr does not support the direct management of dynamic arrays, so you have to customize the delete.

Shared_ptr<int> sp (new int[10], [] (int *p) {delete []p;}];

* (Sp.get () + i) = n;


The copy construction, copy assignment operators are used in different scenarios.

CA a = b; Copy construction;

A = b; Copy assignment operator.

The assignment operator should typically return a reference to its left operand.


p516~p517 page algorithm is good.


Operator Overloading:

Front + + +--()

Post + + +-(int) more than an int to differentiate, do not use this int,;-)


p490:

A unary operator has one argument, and a two-tuple operator has 2. For a two-tuple operator, the left operand is passed to the first argument,

The right operator is passed to the second argument.

If an operator function is a member function, its first (left-hand) operand is bound to the implicit this.

Therefore, the number of (explicit) parameters of the member operator function is one less than the total number of operands of the operator.



Class CA final:public CBase {};

p535 If you have one or more virtual functions in the base class, you can use dynamic_cast to request a type conversion.

If you know that a base class is safe to convert to a derived class, you can use static_cast to force the override of the compiler's check.


The automatic type conversion of a derived class to a base class is valid only for pointers or reference types, and there is no such conversion between 2 objects.


The definition of function templates and class template member functions is usually placed in the header file.

By default, for an instantiated class template, its members are instantiated only when they are used.


C + + Primer 5

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.