Copy-copy constructor of C ++ class objects (deep copy and shallow copy)

Source: Internet
Author: User

Previously, we discussed in our tutorial the problem of generating temporary variables for function returned objects. Next, let's take a look.Return custom type objects in the functionDo you want to follow this rule to generate a temporary object!

Run the following code first:


// Program Author: Guan Ning
// Site: www.cndev-lab.com
// All the manuscripts are copyrighted. If you want to reprint them, be sure to use the famous source and author.

#include <iostream>
usingnamespacestd;

classInternet
{
public:
Internet()
{

};
Internet (char * Name, char * address)
{
Cout <"load constructor" <strcpy (Internet: Name, name );
Strcpy (Internet: Address, address );
}
Internet (Internet & temp)
{
Cout <"load copy constructor" <strcpy (Internet: name, temp. Name );
Strcpy (Internet: Address, temp. Address );
Cin. Get ();
}
~ Internet ()
{
Cout <"load destructor! ";
Cin. Get ();
}
Protected:
Charname [20];
Charaddress [20];
};
Internet TP ()
{
Internet B ("China Software Development Lab", "www.cndev-lab.com ");
Returnb;
}
Voidmain ()
{
Internet;
A = TP ();
}

From the code running result, we can see that the program has loaded the Destructor three times in total, proving that a temporary variable will also be generated when the function returns a custom type object, in fact, object A obtains the Temp value of the Temporary Internet class object.

Let's take a look at the content of this section.Unknown Object.

The object system does not call the copy constructor to initialize an object with an unknown object.

So what is an unknown object?

It is very simple. If the main function of the above program contains:

Internet ("China Software Development Lab", "www.cndev-lab.com ");

Such a statement will generate an unknown object. An unknown object will call the constructor, but the object initialization system will not call the copy constructor!

The following three pieces of code are three examples of initializing objects with unknown objects.


// Program Author: Guan Ning
// Site: www.cndev-lab.com
// All the manuscripts are copyrighted. If you want to reprint them, be sure to use the famous source and author.

#include <iostream>
usingnamespacestd;

Classinternet
{
Public:
Internet (char * Name, char * address)
{
Cout <"load constructor" <strcpy (Internet: Name, name );
}
Internet (Internet & temp)
{
Cout <"load copy constructor" <strcpy (Internet: name, temp. Name );
Cin. Get ();
}
~ Internet ()
{
Cout <"load destructor! ";
Cin. Get ();
}
Public:
Charname [20];
Charaddress [20];
};

Voidmain ()
{
Internet A = Internet ("China Software Development Lab", "www.cndev-lab.com ");
Cout <cin. Get ();
}

The running result of the above Code is a bit "unexpected". In terms of thinking logic, when an unknown object is created, it is necessary to call the custom copy constructor, or, by default, the copy constructor completes the replication process, but the system does not actually do this, because the Untitled object is useless in the whole program after it is used, in this case, C ++ regards the code:
Internet A ("China Software Development Lab", "www.cndev-lab.com ");
The process of creating an unknown object is omitted, so the copy constructor will not be called.

//// // Kevin Lynx //////

Recently I plan to write a linked list template class myself ~~ Then we encountered a very strange problem on the model board ~~ (Xiang see http://bbs.gameres.com/showthread.asp? Page = end & threadid = 49258 )~~ Then I saw the above article ~~ The following is the code I have compiled and the running result is attached. The program runs under Dev -- C ++:

# Include <stdlib. h>
# Include <iostream>
Using namespace STD;

Class Internet
{
Public:
Internet ()
{
Cout <"loading empty constructors" <Endl;
};
Internet (char * Name, char * address)
{
Cout <"load input value constructor" <Endl;
Strcpy (Internet: Name, name );
Strcpy (Internet: Address, address );
}
Internet (Internet & temp)
{
Cout <"load copy constructor" <Endl;
Strcpy (Internet: name, temp. Name );
Strcpy (Internet: Address, temp. Address );
// Cin. Get ();
}
~ Internet ()
{
Cout <"load destructor! "<Endl;
// Cin. Get ();
}
Protected:
Char name [20];
Char Address [20];
};
//////////////
Internet TP ()
{
Internet B ("China Software Development Lab", "www.cndev-lab.com ");
Return B;
}
Int main ()
{
Internet;
Cout <"will call the TP function" <Endl;
A = TP ();
Cout <"will end" <Endl;
System ("pause ");
Return 0;
}

 

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.