Efficient C + +--experience clause (ii)

Source: Internet
Author: User

The STL iterator is shaped as a pointer, so the iterator acts like a t* pointer, declaring that the iterator is const just as if the declaration pointer is const (declaring a t* const pointer), indicating that the iterator must not perform something different, but that the value of what it refers to can be changed. If you want the iterator to be something that cannot be changed (like a const t* pointer), it needs to be const_iterator.

Std::vector<int> VEC;

Const std::vector<int>::iteratoriter = Vec.begin (); ITER acts like a t* const.

*iter = 10; No problem, you can change the object that ITER is referring to.

++iter; Error

Std::vector<int>::const_iteratorciter = Vec.begin (); Citer's role is a constt*.

*citer = 10; Error *citer is const

++citer;//can change citer;

Two member functions can be overloaded if they are just a constant variable.

Mutavble can release bitwiseconstness constraints on Non-static member variables.

An initialization action for a member variable of an object occurs before the constructor body is entered. The member's initialization action is considered to be within the member list.

When you write your own class, we define one or more constructors, a destructor, and a copy assignment operator for the class, for the usual class-defined methods. If you do not declare it, the compiler declares a copy constructor, a copy assignment action symbol, and a destructor for him (compiler version). In addition, if you do not declare any constructors, the compiler will also declare a default constructor for you. All of these functions are public and inline. At the same time, they are only created by the compiler when these functions are required (called). The compiler version generates a copy constructor and a copy assignment operator, and the compiler creates a version that simply copies each non-static member variable from the source object to the target object.

One thing to note, though, is that if you plan to support assignment within a class containing reference members, you have to top up a copy assignment operator yourself. That is, if a member variable in a class has a reference type, the copy assignemnt operator that is automatically generated using the compiler is unreliable. The compiler does not generate a copy assignment operator for a class if there are reference member variables or "contains const members" in the class. If a base classed declares the copy assignment operator as pirvate, the compiler will refuse to generate a copy classes operator for its derived assignment.

In summary: The compiler can secretly create a default constructor, a copy constructor, a copyassignment operator, and a destructor for class. But in some cases the compiler will not do these things. For example, if there is a reference member variable or const member variable in the class, if copy assignment of the base class is declared as private type.

Therefore, if you do not want to use the compiler automatically generated functions, it should be explicitly rejected. If both the copy constructor and the copy assignment operator are declared private and not defined, then the copy constructor and assignment constructor cannot be used externally. In this way, copies and assignments of objects are made.

From this point of extension:

In the Boost library there is a copy of the class, only the subclass inherits the class, then the copy is made, why? Kerry declared a class,

classuncopyable{

Protected:

Uncopuable () {}

~uncopyable () {};

Private:

Uncopyable (const uncopyable&);

uncopyable& operator= (constuncopyable&);

};

If the subclass inherits this class, even if the copy constructor is not declared in the subclass or the copyassignment operator is not copied by the copy constructor or the copy assignment operator, the compiler does not generate it for it. The compiler will simply refuse to generate the default functions for them. This is because the copy function of base class is private.

In order to not allow the compiler to provide these functions automatically, it is possible to declare the corresponding member function as private and not to be implemented, while using a base class such as Uncopyable is also a method.

To this end, you understand what the compiler compiler can do for some classes (no copy constructor defined, no constructor defined, no copy assignment operator defined)! The compiler generates one for it. Also understand when the compiler will not automatically generate the above several functions, but also know what the compiler generated these functions are called to do. At the same time know how to prohibit the compiler to automatically generate when not needed.

Efficient C + +-experience clause (b)

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.