当前位置:网站首页>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 .
边栏推荐
- GBase 8c 函数/存储过程定义
- GBase 8c触发器(三)
- Codeforces Round #418 (Div. 2) D. An overnight dance in discotheque
- Error invalid bound statement (not found): com ruoyi. stock. mapper. StockDetailMapper. XXXX solution
- Distributed transaction solution
- [tutorial] chrome turns off cross domain policies CORS and samesite, and brings cookies across domains
- Gbase 8C system table PG_ constraint
- Apple releases MacOS 11.6.4 update: mainly security fixes
- My creation anniversary
- Return a tree structure data
猜你喜欢
通达OA v12流程中心
random shuffle注意
内存池(内核角度理解new开辟空间的过程)
[Hcia]No.15 Vlan间通信
random shuffle注意
oauth2.0鉴权,登录访问 “/oauth/token”,请求头Authorization(basicToken)如何取值???
PyTorch 卷积网络正则化 DropBlock
Random Shuffle attention
[translation] modern application load balancing with centralized control plane
Choose it when you decide
随机推荐
Distributed transaction solution
Gbase 8C system table PG_ attribute
Choose it when you decide
Gbase 8C system table PG_ cast
Thread safe singleton mode
GBase 8c系统表-pg_amproc
Create + register sub apps_ Define routes, global routes and sub routes
[codeforces] cf1338a - Powered addition [binary]
Gbase 8C function / stored procedure definition
Apple releases MacOS 11.6.4 update: mainly security fixes
[hcia]no.15 communication between VLANs
Exception handling in kotlin process
Gbase 8C system table PG_ class
Gbase 8C system table PG_ auth_ members
[translation] modern application load balancing with centralized control plane
4. 类和对象
GBase 8c 函数/存储过程定义
【Flutter】shared_ Preferences local storage (introduction | install the shared_preferences plug-in | use the shared_preferences process)
Iptables layer 4 forwarding
Gbase 8C system table PG_ am