Copy constructor and value assignment operator details

Source: Internet
Author: User

C ++ has always remembered that "if a class does not define a copy constructor or a value assignment operator, the compiler will generate a copy constructor and a value assignment operator for it ."
This statement is actually vague. I was surprised to see an example when I was reading a book today ".
# Include <stdio. h>
Struct X {X (){};
Template <typename T> X (const T &) {printf ("templated X copy constructor .");}
Template <typename T> X & operator = (const T &) {printf ("templated X assignment operator .");}};
Int main (int argc, char ** argv) {X x1; X x2 (x1); X x3; x3 = x1 ;}
After this program is compiled, what is the execution result? Answer: No! Neither of the two function templates is called.
The reason is not very complex, but it can be seen that I am still not clear about the details of C ++. Okay, now the details are coming:
C ++ standards (ISO/IEC 14882: 2003 (E) 12.8.2 are clearly written: A non-template constructor for class X is a copy constructor if its first parameter is of type X &, const X &, volatile X & or const volatile X &, and either there are no other parameters or else all other parameters have default arguments (8.3.6 ).
Key Point: A function template with template parameters is not a constructor.
Therefore, the template <typename T> X (const T &) declared in the preceding example is not a copy constructor.
In addition, according to the definition of the copy constructor, the class definition above does not explicitly define any copy constructor.
C ++ standards: If the class definition does not explicitly declare a copy constructor, one is declared implicitly. This is basically something that people who have learned C ++ know. Therefore, the compiler must generate an implicit copy constructor for the above class.
The implicit copy constructor generated by the compiler performs a shortest copy on all the members of the class without any output.
The assignment operator is similar. Standard definition: A user-declared copy assignment operator X: operator = is a non-static non-template member function of class X with exactly one parameter of type X, X &, const X &, volatile X & or const volatile X &. that is, the value assignment operator must be a non-template, non-static member function, and can only have one parameter. This is different from the copy constructor. A copy constructor can have multiple parameters, as long as the first one is X & or cv-qualified X &, and all subsequent parameters have default values.
When the program is actually executed, neither of the two function templates is called, so there is no output.

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.