C ++ learning constructor and copy constructor, learning copy constructor
Extended Question 1: whether the const parameter is added to the copy constructor affects the copy constructor.
Most people on the internet can only explain this problem to the extent that "when you do not want to modify the parameter, you need to add the const keyword, however, there is no difference between the two situations. The following procedure is used as an example:
Dog. h
#ifndef __test_header__Dog__#define __test_header__Dog__#include <stdio.h>class Dog{public: Dog(); Dog(Dog &dog);};#endif
Dog. cpp
#include "Dog.h"#include <iostream>using namespace std;Dog::Dog(){ cout<<"Dog()"<<endl;};Dog::Dog(Dog &dog){ cout<<"Dog(Dog &dog)"<<endl;};
Main. cpp
#include <iostream>#include <string>#include "Dog.h"using namespace std;int main(int argc, const char * argv[]) { // insert code here... Dog dog1; Dog dog2 = dog1; return 0;}
The output result is:
If you change Dog dog1 to const Dog dog1, an error will occur after compilation.
The prompt shows that no constructor is matched. This is because we have not defined a const Dog & dog copy constructor parameter.
So what happens if we modify the program to the following code?
Dog. h
#ifndef __test_header__Dog__#define __test_header__Dog__#include <stdio.h>class Dog{public: Dog(); //Dog(Dog &dog); Dog(const Dog &dog); //Dog& operator=(Dog &dog);};#endif
Dog. cpp
#include "Dog.h"#include <iostream>using namespace std;Dog::Dog(){ cout<<"Dog()"<<endl;};Dog::Dog(const Dog &dog){ cout<<"Dog(const Dog &dog)"<<endl;};
Main. cpp
#include <iostream>#include <string>#include "Dog.h"using namespace std;int main(int argc, const char * argv[]) { // insert code here... Dog dog1; Dog dog2 = dog1; return 0;}
Will there be a compilation error because there is no copy const without const? The answer is no. Because we provide a copy constructor with the const keyword, both const Dog dog1 and Dog dog1 can use the copy constructor with the const keyword, because the copy constructor with const is more strict than the copy constructor without const, the acceptable parameter can be a const constant or a variable without const, this is a bit backward compatible. In this case, if no const copy constructor is found, the const copy constructor will be called.
Now you may have to ask, what if two copy constructors with and without const are provided at the same time? Modify Main. cpp as follows:
#include <iostream>#include <string>#include "Dog.h"using namespace std;int main(int argc, const char * argv[]) { // insert code here... Dog dog1; const Dog dog2; Dog dog3 = dog1; Dog dog4 = dog2; return 0;}
The answer is:
When Dog dog1 is used as the right operand of the equal sign, a copy constructor without const is called.
When const Dog dog1 is used as the right operand of the equal sign, a copy constructor with const is called.
Next, let's talk about the default copy constructor provided by the system. The default copy constructor of the system has two possibilities:
First, only copy constructors with const are provided, because they are compatible with parameters without const.
Type 2: Depending on the situation, if the parameter has const, a copy constructor is constructed. If the parameter does not contain const, a function without const is constructed.
But it doesn't make much sense.