C++ study notes (4) copy constructor

#include <bits/stdc++.h>
using namespace std ;

class Student {
public:
	int NO ;
	string name ;

	explicit Student ( const int _NO , const string _name )
	: NO ( _NO ) , name ( _name ) {
		cout << endl << "Constructor" << endl ;
	}

	void display () const {
		cout << endl << "NO  :  " << NO << "\t" << name << endl ;
	}

	~Student () noexcept {}
} ;

int main() {

	Student One ( 1 , string ( "Jack" ) ) ;
	One.display () ;

	Student Two ( One ) ; // make a copy
	Two.display () ;

	Student Three = One ; // make a copy
	Three.display () ;

	vector < Student > P ; // Putting into the container will call the copy constructor
	P.push_back ( One ) ;
	P[0].display () ;

	vector < Student* > Q ;
	Q.push_back ( &One ) ; // The copy constructor is not called here....because it's a pointer
	Q[0]->display () ;

	One.name = string ( "Marry" ) ; // Operations on pointers will apply to the original memory
	Q[0]->display () ;

	return 0 ;
}

C++ classes have a default copy constructor, which, like the constructor, is automatically generated by the compiler if the programmer does not declare it.

A few things that are easy to go wrong are:

1. When an object entity is placed in the container, the copy constructor will be called

2. The pointer of the object entity is placed in the container, and the copy constructor will not be called, because the pointer is only the address

3. For some functions of the container, the parameters are of const type, so if you write a copy constructor yourself, the parameters must be of const type.

For example, C++ vector : 

push_back ( const value_type & )
The requirement is of type const.

C++11 also supports rvalue references, push_back ( value_type && )

4. Adding const can prevent the source object from being modified during the copy process

5. Adding & is a reference. In order to reduce the copy constructor, because if no reference & is added, the incoming parameter itself is a copy, and the copy constructor will also be called, so it will be called infinitely

6. When the object entity is very large, it is not recommended to use the copy constructor because the overhead is too high. You can choose to use a pointer, you only need to save an address, and in the case of a base class, you can also use pointers to achieve polymorphism, but the operations of pointers are all performed on the source object, which should be noted.

7. Copy constructor, by default, look for non-const types first, then const types

#include <bits/stdc++.h>
using namespace std ;

class Student {
public:
	int NO ;
	string name ;

	explicit Student ( const int _NO , const string _name )
	: NO ( _NO ) , name ( _name ) {
		cout << endl << "Constructor" << endl ;
	}

	void display () const {
		cout << endl << "NO  :  " << NO << "\t" << name << endl ;
	}

	// vector
	// push_back ( const value_type & )
	Student ( const Student &One ) { // Container push_back parameter is const
		this->NO = One.NO ;
		this->name = One.name ;
		cout << endl << "The copy constructor whose parameter is of type const was called" << endl ;
	}

	Student ( Student &One ) {    // explicit
		this->NO = One.NO ;
		this->name = One.name ;  
		cout << endl << "The copy constructor whose argument is a normal data type is called" << endl ;
	}

	Student ( Student *One ) {
		this->NO = One->NO ;
		this->name = One->name ;
		cout << endl << "The copy constructor whose argument is a pointer type was called" << endl ;
	}

	~Student () noexcept {}
} ;

int main() {

	Student One ( 1 , string ( "Jack" ) ) ;
	One.display () ;

	Student Two ( One ) ; // make a copy
	Two.display () ;

	vector < Student > P ; // Putting into the container will call the copy constructor
	P.push_back ( One ) ;
	P[0].display () ;

	vector < Student* > Q ;
	Q.push_back ( &One ) ; // The copy constructor is not called here....because it's a pointer
	Q[0]->display () ;

	One.name = string ( "Marry" ) ; // Operations on pointers will apply to the original memory
	Q[0]->display () ;

	return 0 ;
}



Guess you like

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