Class ?. XML

Source: Internet
Author: User
Pre {Line-Height: 1; color: # 1e1e1e; Background-color: # d2d2d2; font-size: 16px ;}. sysfunc {color: #627cf6; font-style: italic; font-weight: bold ;}. selffuc {color: #800080 ;}. bool {color: # d2576f ;}. condition {color: #000080; font-weight: bold ;}. key {color: #000080 ;}. vaR {color: #800000; font-style: italic ;}. digit {color: # ff00ff; font-weight: bold ;}. includepre {color: # 1e1e1e ;}. operator? {Color: #008000; font-weight: bold ;}

Class

???? All classes ?? There is only one destructor and a value assignment function ???????????? But there can be multiple constructors ??? For any class, the C ++ compiler automatically generates four default functions :??????? A ()?? //? The default no-argument constructor ??? If any constructor is defined, the default constructor is invalid ??????? A (const? A? & );? //? The default copy constructor is the shortest copy ?????? A &? Operator = (const? A? & )?;? //? Default Value assignment function ?????~ A (); default destructor ??

By type

?

1? Default constructor

?? A ();????????? The default constructor generated by the compiler?

2? Copy constructor

??? If there is only one constructor parameter and the parameter type is set to this type, the constructor is copied .?? For example, a (A &) A (const? A &) a (A &, Int? I = 0 )??? // If other parameters have default values, is this constructor also a copy constructor?

2.1? Default copy constructor

?? A (const? A? & );??? Shallow copy ?? Normal copy ?? Which of the following is a simple one-to-one assignment of new objects with the data of?

2.2? Deep copy

? A (const? A & )?;?? Dynamic space allocation?

When will the copy function be called?

?

1 )????????? Initialize with another object

A??a;A??b=a;A??c(a);?

2) value transfer

???Void?Fun(A?a){}?

3 )???????? The object is returned from the function by passing the value.

?A?Fun(){A?a;Return?a;}??

3? Conversion Constructor

?? Similar to the copy constructor, there is only one parameter, but this parameter is not the type of this class but other types .?? A (B? A ){};? A? A; B? BA = B ;??? //?? Execute the conversion function? A (B )????

By restricted access

??

The constructor is generally of the Public type, but in special cases, the private type is used.

????

1? Singleton Mode

??? Privatize all constructors ???????? Code ;:? // Singleton. hclass? Singleton? {Public :??????? Static? Singleton *? Getinstance (); Private :??????? Singleton ()? {}??????? Static? Singleton? * Singleton;}; // singleton. cppsingleton *? Singleton: Singleton? =? NULL; Singleton *? Singleton: getinstance (){??????? If (Singleton? ==? Null )?????????????? Singleton? =? New? Singleton ();??????? Return? Singleton ;}?

2? Private copy constructor? Prevent default copy

?

What do you want to do ?? The constructor is copied during initialization, value transfer, and return value.

Used in this article? Calligraphy and painting novel software? Released, the content has nothing to do with the software, and the painting and calligraphy novel software? More comfortable reading, more comfortable writing, and easier Publishing.

Class ?. XML

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.