Learning C ++ constructor and Destructor from scratch (3): Deep copy and shallow copy, empty class

Source: Internet
Author: User

I. Deep copy and light copy

To put it simply, assume that a class has pointer members. If the memory to which the Pointer Points is also allocated during the copy, it is called Deep copy, for example (V2 is copied from V ):


If only the memory of the pointer is allocated, it is a shallow copy, for example:


The problem caused by the shallow copy is that two pointers point to the same block of memory, and one of the delete pointers, the remaining pointers will become wild pointers. By default, the copy constructor and the value assignment operator synthesized by the compiler are shortest copies. If only the assignment of common members is used, shortest copies are supported.


C ++ code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# Ifndef _ string_h _
# DEFINE _ string_h _

Class string
{
Public:
String (char * STR = "");
~ String ();
String (const string & other );
String & operator = (const string & other );

Void display ();

PRIVATE:
Char * allocandcpy (char * Str );

Char * STR _;
};

# Endif // _ string_h _

C ++ code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# Include "string. H"
// # Include <string. h>
# Include <cstring>
# Include <iostream>
Using namespace STD;

String: string (char * str/* = */)
{
STR _ = allocandcpy (STR );
}

String ::~ String ()
{
Delete [] STR _;
}

String: string (const string & other)
{
STR _ = allocandcpy (other. Str _);
}

String & string: Operator = (const string & other)
{
If (this = & other)
Return * this;

Delete [] STR _;
STR _ = allocandcpy (other. Str _);
Return * this;
}

Char * string: allocandcpy (char * Str)
{
Int Len = strlen (STR) + 1;
Char * TMP = new char [Len];
Memset (TMP, 0, Len );
Strcpy (TMP, STR );
Return TMP;
}

Void string: Display ()
{
Cout <STR _ <Endl;
}

C ++ code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# Include "string. H"

Int main (void)
{
String S1 ("AAA ");
S1.display ();
String S2 = S1; // call the copy constructor.
// The default copy constructor provided by the system implements the shortest copy s2.str _ = s1.str _

String S3;
S3.display ();
S3 = S2; // call the equal sign Operator
// The default equal sign operator provided by the system implements the shortest copy s3.str _ = s2.str _;
// S3.operator = (S2 );
S3.display ();
// To make the object unique, We must Disable copying.
// The method is to declare the copy constructor and the = operator as private and do not provide their implementation.
Return 0;
}

In the above program, the string class has a char * STR _ member, so deep copy is implemented, which will not cause the error of memory being released twice, or modifying the memory pointed to by the pointer will affect the error of another object. In addition, if we want to make objects unique and prohibit copying, we only need to declare the copy constructor and equal sign operator as private and do not provide their implementation.

Ii. Empty category

Default members of empty classes:

Class empty {};
Empty (); // default constructor
Empty (const empty &); // default copy constructor
~ Empty (); // default destructor
Empty & operator = (const empty &); // default value assignment operator
Empty * operator & (); // address fetch Operator
Const empty * operator & () const; // address fetch operator const

C ++ code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# Include <iostream>
Using namespace STD;

Class empty
{
Public:
Empty * operator &()
{
Cout <"aaaa" <Endl;
Return this;
}

Const empty * operator & () const
{
Cout <"BBBB" <Endl;
Return this;
}
};

Int main (void)
{
Empty E;
Empty * P = & E; // equivalent to E. Operator &();

Const empty E2;
Const empty * P2 = & E2;

Cout <sizeof (empty) <Endl;

Return 0;
}


After one-step debugging, we can see that two address fetch operator functions are called respectively, and the empty class size is 1 byte.

Refer:

C ++ primer version 4
Valid tive C ++ 3rd
C ++ programming specifications

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.