C + + constructors

Source: Internet
Author: User

Reference from http://blog.csdn.net/muzi9_17/article/details/6776061 and effective C + +

1. Default constructors

As long as you define your own constructor, the compiler does not automatically generate a default constructor. The main function of the default constructor is to allocate space to the object, and cannot initialize the data. Also, for example, object (void) and object (int = 0) are the same default constructors. This is the default constructor for the basic type initial value, even if the latter has its own defined operation. So you have to define your own constructor to complete the initialization of the data.

2. Default copy constructor

If the user does not define a copy constructor, the compiler automatically adds a default copy constructor (with a parameter of that class type), called a shallow copy. So-called shallow copy http://blog.csdn.net/lwbeyond/article/details/ 6202256, refers to when the object is copied, only the data members in the object is simply assigned, the default copy constructor is also a shallow copy of the execution. In most cases a "shallow copy" has worked well, but once the object has a dynamic member, a shallow copy can be problematic. If a class has data types such as dynamic arrays, a shallow copy can be problematic. So the copy constructor is also defined by itself.

3.copy assignment operator L

If you do not define it, it is also automatically generated by the system, but when self-assignment causes two pointers to point to the same object, the other disappears as soon as the memory is freed after one destructor. can cause problems. It is possible to introduce a certificate and test. However, there is no exception security, because in the dynamic allocation of memory, the failure may be an exception, the final object will hold a pointer to a piece of deleted memory, such a pointer is harmful, cannot be safely deleted. So like this, do not delete this before copying this point.

cmystring& cmystring::operator= (const cmystring& STR)
{
if (this = &str) {
CMyString strtmp (str);

char* ptmp = Strtmp.m_pdata;
Strtmp.m_pdata = M_pdata;
M_pdata = ptmp;
}

return *this;

}//Sword means the offer code creates a temporary instance and then switches the pointer and exits if the temporary instance is automatically broken down by the way, releasing the original instance memory. Very good

4. The difference between copy constructor and copy assignment operator

Http://blog.sina.com.cn/s/blog_48d4d2df010002n9.html

For example

Widget W2 (W1); Invoke copy constructorw1 = W2; Invoke copy//assignment operator

When you see something that looks like an assignment, read it carefully, because "=" can also be used to invoke the copy constructor in syntax: (Why do I think the default constructor is called)
Widget W3 = W2; Invoke Copy constructor!

Fortunately, the copy constructor is easily distinguishable from the copy assignment. If athe new object is defined(Just like the W3 in the above line of code), a constructor must be called; it cannot be an assignment. If no new object is defined (as in the line "w1 = W2" code above), no constructor can be called, so it is an assignment. Therefore, you should try to initialize with a custom constructor, or initialize it with a copy constructor, because copy assignment Operater may invoke a construct once, operator.

C + + constructors

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.