当前位置:网站首页>Classes and objects - initialization and cleanup of objects - constructor call rules
Classes and objects - initialization and cleanup of objects - constructor call rules
2022-07-03 02:31:00 【Python's path to immortality】
By default ,c++ The compiler adds... To at least one class 3 A function
- Default constructor ( No arguments , Function body is empty )
- Default destructor ( No arguments , Function body is empty )
- Default copy constructor , Copy the value of the property
The rules for calling constructors are as follows :
- If the user defines a parameter constructor ,c++ No default parameterless construction is provided , But the default copy structure will be provided
- If you define a copy constructor ,c++ No more constructors
#include<iostream>
using namespace std;
// Call rules for constructors
//1、 Create a class ,C++ The compiler adds at least 3 A function
// Default structure ( Empty implementation )
// Destructor ( Empty implementation )
// Copy structure ( Value copy )
//2、
// If we write a parametric constructor , The compiler no longer provides a default construct , Copy structure will still be provided
// If we write a copy constructor , The compiler will no longer provide other ordinary constructors
class Person {
public:
/*Person() { cout << "Person The default constructor for calls " << endl; }*/
/*Person(int age) { cout << "Person The parameterized constructor of " << endl; m_Age = age; }*/
Person(const Person& p) {
cout << "Person Copy constructor call for " << endl;
m_Age = p.m_Age;
}
~Person() {
cout << "Person Destructor call for " << endl;
}
int m_Age;
};
//void test01() {
// Person p1;
// p1.m_Age = 18;
//
// Person p2(p1);
//
// cout << "p2 The age is :" << p2.m_Age << endl;
//}
void test02() {
//Person p3(28);
//Person p4(p3);
//cout << "p4 The age is :" << p4.m_Age << endl;
}
int main() {
//test01();
//test02();
system("pause");
return 0;
}
By default ,c++ The compiler adds... To at least one class 3 A function
- Default constructor ( No arguments , Function body is empty )
- Default destructor ( No arguments , Function body is empty )
- Default copy constructor , Copy the value of the property
class Person {
public:
Person() {
cout << "Person The default constructor for calls " << endl;
}
Person(int age) {
cout << "Person The parameterized constructor of " << endl;
m_Age = age;
}
Person(const Person& p) {
cout << "Person Copy constructor call for " << endl;
m_Age = p.m_Age;
}
~Person() {
cout << "Person Destructor call for " << endl;
}
int m_Age;
};
void test01() {
Person p1;
p1.m_Age = 18;
Person p2(p1);
cout << "p2 The age is :" << p2.m_Age << endl;
}
int main(){
test01();
return 0;
}
Running results 
Comment out the copy constructor
class Person {
public:
Person() {
cout << "Person The default constructor for calls " << endl;
}
Person(int age) {
cout << "Person The parameterized constructor of " << endl;
m_Age = age;
}
/*Person(const Person& p) { cout << "Person Copy constructor call for " << endl; m_Age = p.m_Age; }*/
~Person() {
cout << "Person Destructor call for " << endl;
}
int m_Age;
};
void test01() {
Person p1;
p1.m_Age = 18;
Person p2(p1);
cout << "p2 The age is :" << p2.m_Age << endl;
}
int main(){
test01();
return 0;
}
Running results 
p2 Still show age , No Person Copy constructor for , There was no mistake , Therefore, the compiler provides a default copy constructor , namely m_Age = age;
The rules for calling constructors are as follows :
- If the user defines a parameter constructor ,c++ No default parameterless construction is provided , But the default copy structure will be provided
- If you define a copy constructor ,c++ No more constructors
1. If the user defines a parameter constructor ,c++ No default parameterless construction is provided , But the default copy structure will be provided
// Provide a parameter constructor , But no default constructor is provided
class Person {
public:
/*Person() { cout << "Person The default constructor for calls " << endl; }*/
Person(int age) {
cout << "Person The parameterized constructor of " << endl;
m_Age = age;
}
/*Person(const Person& p) { cout << "Person Copy constructor call for " << endl; m_Age = p.m_Age; }*/
~Person() {
cout << "Person Destructor call for " << endl;
}
int m_Age;
};
void test02(){
Person p3;
}
int main(){
test02();
return 0;
}
Error will be reported at this time 
Because we provide a parameterized constructor , All compilers no longer provide default constructors , So there will be an error
// Provide a parameter constructor , But copy constructors are not provided
class Person {
public:
/*Person() { cout << "Person The default constructor for calls " << endl; }*/
Person(int age) {
cout << "Person The parameterized constructor of " << endl;
m_Age = age;
}
/*Person(const Person& p) { cout << "Person Copy constructor call for " << endl; m_Age = p.m_Age; }*/
~Person() {
cout << "Person Destructor call for " << endl;
}
int m_Age;
};
void test02(){
Person p3(28);
Person p4(p3);
cout<< "p4 The age is :" << p4.m_Age << endl;
}
int main(){
test02();
return 0;
}
Running results 
Provide its own constructor with parameters , Even if we don't provide copy constructors , The compiler also provides a default copy constructor
- If you define a copy constructor ,c++ No more constructors
// Provide copy constructor , But ordinary constructors are not provided
class Person {
public:
/*Person() { cout << "Person The default constructor for calls " << endl; }*/
/*Person(int age) { cout << "Person The parameterized constructor of " << endl; m_Age = age; }*/
Person(const Person& p) {
cout << "Person Copy constructor call for " << endl;
m_Age = p.m_Age;
}
~Person() {
cout << "Person Destructor call for " << endl;
}
int m_Age;
};
void test02(){
Person p3;
}
int main(){
test02();
return 0;
}
Error will be reported at this time 
Because we provide a copy constructor , But no default constructor is provided , The compiler also does not provide a default constructor , So it will report a mistake .
summary : I wrote the parametric structure , The compiler does not provide a default construct , Will provide copy construction ; Wrote a copy structure , Default and parametric constructs are not provided .
边栏推荐
- JS的装箱和拆箱
- [fluent] JSON model conversion (JSON serialization tool | JSON manual serialization | writing dart model classes according to JSON | online automatic conversion of dart classes according to JSON)
- Solution for processing overtime orders (Overtime unpaid)
- COM and cn
- Error when installing MySQL in Linux: starting mysql The server quit without updating PID file ([FAILED]al/mysql/data/l.pid
- 通达OA v12流程中心
- GBase 8c系统表-pg_amproc
- 【 tutoriel】 Chrome ferme les cors et les messages de la politique inter - domaines et apporte des cookies à travers les domaines
- Job object of collaboration in kotlin
- Cvpr2022 remove rain and fog
猜你喜欢
随机推荐
The use of Flink CDC mongodb and the implementation of Flink SQL parsing complex nested JSON data in monggo
Kotlin middle process understanding and Practice (II)
awk从入门到入土(0)awk概述
[translation] the background project has joined the CNCF incubator
GBase 8c系统表-pg_amop
CFdiv2-Fixed Point Guessing-(區間答案二分)
Y54. Chapter III kubernetes from introduction to mastery -- ingress (27)
awk从入门到入土(1)awk初次会面
Interview stereotyped version
Detailed introduction to the deployment and usage of the Nacos registry
UDP receive queue and multiple initialization test
Unrecognized SSL message, plaintext connection?
4. Classes and objects
【 tutoriel】 Chrome ferme les cors et les messages de la politique inter - domaines et apporte des cookies à travers les domaines
Solution for processing overtime orders (Overtime unpaid)
[advanced ROS] Lesson 6 recording and playback in ROS (rosbag)
Qt之QComboBox添加QCheckBox(下拉列表框插入复选框,含源码+注释)
Iptables layer 4 forwarding
Simple understanding of SVG
Gbase 8C system table PG_ collation








