The constructor and copy control of c++--class

Source: Internet
Author: User
Tags class definition

The shift between class and struct (struct) differs in that: By default, a class is derived and access is private, the struct's derivation and access rights is public.

constructor Function

  The task of the constructor is to initialize the data members of the class object and execute the constructor whenever the object of the class is created.

Feature 1: Unlike other member functions, constructors cannot be declared as const (see 7.1.2,P231).

When we create a const object of a class, the object can actually get its "constant property" until the constructor finishes the initialization process. Therefore, a constructor can write a value to a const object during its construction.

Const Sales_data obj (); Const Object

Const member functions:

  The function of const is to modify the type of the implicit this pointer . By default, the type of this is a constant pointer to a very version of the class type, as in the Sales_data member function, the type of this is sales_data *const. This means that, by default, this cannot be bound to a constant object. Therefore, the member functions that "do not modify class objects" should be defined as const. such as String ISBN ();

1 string Const   // This type is const sales_data *const 2 {returnthis->ISBN;}

compositing default constructors

If you do not explicitly define a constructor, the compiler implicitly defines a composite default constructor for us.

  initialization Rule : 1, if there is an initial value within the class (with a default value), use it to initialize the member, or 2, otherwise, the member is initialized by default.

Some classes, such as those that contain type members that cannot rely on default initialization, cannot depend on the composition's default constructor. You typically customize the default constructor.

default Constructor (=default)

 You typically define a default constructor (=default) for a class:

1 struct Sales_data 2 {3     Sales_data () =default;         // His role is exactly the same as compositing the default constructor 4     Sales_data (conststring &s): Bookno (s) {}5    ...   6 }

  in one case, the default constructor for "=defalut" must be removed : If a constructor provides default arguments for all parameters, it becomes the default constructor.

If there is a version of the original "=default", in "Sales_data A;" , the compiler does not know which version of the constructor to call, and "ambiguity" appears.
Constructor Initial Value List

If a member is a const, a reference , or a class type that does not provide a default constructor , we must provide the initial values for those members through the constructor initializer list.

 //  constructor, version 1  sales_data::sales_data ( const  string  &s,unsigned n,double   p) {Bookno  =s;    Units_sold  =n; Revenue  =n*p;}  //  constructor, version 2  Sales_data (const  string  &s,unsigned N , double   P): Bookno (s), Units_sold (n), revenu E (P  *n) {} 

Version 2 takes a list of initial values and the two versions have the same effect. The difference is that version 2 initializes his data members, and two version 1 performs assignment operations on data members.
the order in which members are initialized: consistent with the order in which they appear in the class definition, the first member is initialized first, then the 2nd, and so on. The pre-and post-position of the initial values in the constructor initializer list does not affect the actual initialization order.

five Special member functions: copy constructor , copy assignment operator , move constructor , move assignment operator , and destructor .

Copy constructors and Mobile dog manufacturing functions define what to do when initializing this object with another object of the same type . the copy and move assignment operators define what to do when one object is assigned to another object of the same type . destructors define what to do when this type of object is destroyed .

The constructor and copy control of c++--class

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.