Principle and Implementation of non-replicable classes

Source: Internet
Author: User

The main principles are as follows: 3 and 4

I. copy constructors and value assignment operators
Class;

A;

A B1 (a); // copy the constructor instance

A b2 = A; // an example of the value assignment operator:

Ii. Copy the application instance of the constructor
1. display or implicitly Initialize an object of the same type, as shown below:
Class;

A;

A B ();

2. As the real parameter of the function, as follows:
Void work (const A & );
3. As the return value of the Function
A & geta () const
{

...

Return;
}
4. initialize elements of the sequential container
5. initialize array elements based on the element initialization list

3. The copy constructor for merging and the assignment operator for merging are as follows:

If you do not explicitly define a copy constructor or a value assignment operator, the compiler will synthesize a copy constructor by default. The merging and copying constructor and the merging and assignment operators are different from the assignment operators. By default, the execution behavior of the function synthesized by the compiler is "initialize members one by one ", initialize the new object as a copy of the original object. One-by-one initialization does not include static members, but is only responsible for initializing non-static members.

4. as mentioned above, to prevent copying and assigning values, the class can display and declare that its copy constructor and value assignment operator are private. If the copy constructor is private, user code will not be allowed to copy objects of this type, and the compiler will reject any replication attempts. At this time, the friends and members of the class can still be copied. Of course, it is easy to disable this situation. The following code does not support copying classes:

 

class NonCopyable{    protected:            NonCopyable(){}            ~NonCopyable(){}    private:        NonCopyable(const NonCopyable &);        const NonCopyable& operator=(const NonCopyable&);};

 

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.