copy constructor


Why copy constructor parameters can only be used by reference?

This starts with the trigger when the copy constructor is digital. The copy constructor will be automatically called in the following situations:

1) When initializing a new object with an existing object

2) When passing an object by value to a formal parameter

3) When the function returns an object

So when an object is passed a function by value, the copy constructor is automatically called to generate the object in the function. If an object is passed its own copy constructor, its copy constructor will be called to copy the object so that the copy can pass its own copy constructor, which will cause an infinite loop until the stack overflows except when the object In addition to being called implicitly when passed into a function, the copy constructor is also called when the object is returned by the function. (Excerpted from Baidu Encyclopedia  copy constructor  ).

Copy constructors generally do not need to be written by yourself. The default copy constructor of the system can withstand it. However, in some cases, you need to open up space during construction. In this case, you need a copy constructor.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324695689&siteId=291194637