The operands of predefined operators in C + + can only be basic data types, and in fact, for many user-defined types, similar operations are Required. For example:Class Complex{PublicComplex (double r=0.0,double i=0.0) {real=r;imag=i;}void Display ()
C ++ from scratch (4)
-- Value assignment operator
This article is a continuation of "C ++ from scratch (ii)" and describes the content of expressions left over from "C ++ from scratch (ii, it also paves the way for the use of pointers in the next
First, make it clear:
The system has provided the default copy constructor and the = copy operator. That is, the so-called shallow copy.
But sometimes we have to provide our own rewrite. Generally, it is rewritten with pointers.
For a simple example,
First, make it clear:
The system has provided the default copy constructor and the = copy operator. That is, the so-called shallow copy.
But sometimes we have to provide our own rewrite. Generally, it is rewritten with pointers.
For a simple example,
Move semanticsCompletion of the transfer of ownership, when the copy construction and assignment construction, the target object ownership must be handed over to our new object, the original object will lose ownership, the _p pointer will no longer
Found a good article:Reproduced:This paper mainly summarizes the operator overloading and C + + operator overloading summary from C + +.What is operator overloadingOperator overloading is to give multiple meanings to existing operators, causing
The assignment operator overload function of a custom class acts like a built-in assignment operator, note, however, that it is the same as a copy constructor and a destructor to note the problem of deep copy of a shallow copy, and without a deep
Note: classes that contain dynamically assigned members should provide copy constructors and overload the "=" value assignment operator.
The following examples will be used:
Class cexample {public: cexample () {pbuffer = NULL; nsize = 0 ;}~ Cexample
C ++-copy constructor, copy-assignment operator, destructor, assignmentoperator
For a class, copy constructor, copy-assignment operator, move constructor, move-assignment operator, and destructor are collectively referred to as copy control.
Let's
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.