当前位置:网站首页>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 .
边栏推荐
猜你喜欢

udp接收队列以及多次初始化的测试

UDP receive queue and multiple initialization test
![[translation] modern application load balancing with centralized control plane](/img/b0/22e9bf098d580b2af67255ddcdc0d5.jpg)
[translation] modern application load balancing with centralized control plane

Servlet中数据传到JSP页面使用el表达式${}无法显示问题
![[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)](/img/6a/ae44ddb090ce6373f04a550a15f973.jpg)
[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)

Awk from introduction to earth (0) overview of awk

oauth2.0鉴权,登录访问 “/oauth/token”,请求头Authorization(basicToken)如何取值???

Tongda OA V12 process center

定了,就选它

线程安全的单例模式
随机推荐
Cfdiv2 fixed point guessing- (interval answer two points)
Gbase 8C system table PG_ auth_ members
CFdiv2-Fixed Point Guessing-(區間答案二分)
Servlet中数据传到JSP页面使用el表达式${}无法显示问题
Startup mode and scope builder of collaboration in kotlin
4. 类和对象
GBase 8c触发器(三)
Error when installing MySQL in Linux: starting mysql The server quit without updating PID file ([FAILED]al/mysql/data/l.pid
Memory pool (understand the process of new developing space from the perspective of kernel)
GBase 8c系统表-pg_am
Cvpr2022 remove rain and fog
Current situation and future of Web3 in various countries
Codeforces Round #418 (Div. 2) D. An overnight dance in discotheque
Return a tree structure data
GBase 8c 函数/存储过程参数(二)
Qt之QComboBox添加QCheckBox(下拉列表框插入复选框,含源码+注释)
[Hcia]No.15 Vlan间通信
[codeforces] cf1338a - Powered addition [binary]
Gbase 8C system table PG_ conversion
Pytorch convolution network regularization dropblock