当前位置:网站首页>Constructor, copy function and destructor
Constructor, copy function and destructor
2022-07-27 01:46:00 【Count of Jiangnan】
Constructors , The difference between copy function and destructor
The default function
because c++ Mainly for object-oriented , So it has three basic characteristics : encapsulation , Inherit , polymorphic , and c++ The six default functions in the class of are mainly used to encapsulate the process , namely : Constructors , Copy function , Destructor , Overloading of assignment operands , Overload of address operator ,const The modified address fetching operator overloads
Here we mainly talk about constructors , Copy function , The difference between destructors
Constructors
The function name of the constructor must have the same name as the class name , And the constructor has no return value , The main function of constructor is to initialize function
class A {
public:
A(int a,int b,char c) {
_a = a;
_b = b;
_c = c;
}
int _a;
int _b;
char _c;
};
int main() {
A i(1,2,'s');
cout << i._a << i._b << i._c << endl;
return 0;
}
Let's look at the code above , You can see that we have defined a class type A, The object is created in the main function i, And put 1,2,s The data is transmitted to _a,_b,_c Member variables , And in the class A Functions are constructors . We should note that , Class _a,_b,_c Variables are just Declare objects , There is no room to create objects , They are in Constructors A Initializing objects in
So if we don't create constructors , Can it be successful ?
The answer is yes , Because the compiler will have its own default constructor , The default constructor is the compiler's automatic initialization function
class A {
public:
int _a=1;
int _b=2;
char _c='s';
int main() {
A i;
cout << i._a << i._b << i._c << endl;
return 0;
};
Here is to initialize the default constructor directly , The output of the above code is also 12s, But we can use a more convenient default constructor , This is also the default constructor
class A {
public:
A(int a = 1, int b = 2, char c = 's') {// Default constructor
_a = a;
_b = b;
_c = c;
}
int _a;
int _b;
char _c;
};
int main() {
A i1(3,4,'t');
A i2;
cout << i1._a << i1._b << i1._c << endl;
cout << i2._a << i2._b << i2._c << endl;
return 0;
}
The code output above is 34t and 12s, When transferring parameters, use the parameter value , If there is no parameter transfer, the default initialization
It is worth noting that , When we don't write a constructor , The compiler default constructor will have an eccentric treatment :
1、 Built in types are not initialized 2、 The custom type will call its constructor to initialize , If the custom type has no constructor , It will not be initialized
Destructor
Destructor has no return value , No parameters , Destruct name adds a before the class name ~, If the class is undefined , The compiler will automatically generate the default destructor , At the end of the object life cycle , The destructor is automatically called
The main function of the destructor is to release such as malloc Objects that open up memory space on the heap , Clean up
class Stack {
public:
Stack(int capacity = 4) {
_a = (int*)malloc(sizeof(int) * capacity);
if (_a == nullptr) {
cout << "malloc fail" << endl;
exit(-1);
}
_top = _capacity = 0;
}
~Stack() {
free(_a);
_a = nullptr;
_top = _capacity = 0;
}
int* _a;
int _top;
int _capacity;
};
int main() {
Stack i;
return 0;
}
Copy function
no return value , Take the class name as the function name
class A {
public:
A(int a = 1, int b = 2, char c = 's') {
_a = a;
_b = b;
_c = c;
}
A(const A& d) {
// Copy function
_a = d._a;
_b = d._b;
_c = d._c;
}
int _a;
int _b;
char _c;
};
int main() {
A i;
A i2(i);//A i2=i; Also call the copy function
cout << i2._a << i2._b << i2._c << endl;
return 0;
}
After debugging , We can find out A i2(i); The code here calls the copy function It is worth noting that , Here, the parameters of the copy function pass references , signify d It's a class A Of i References to , If there is no quotation , That will cause me to copy the definition in the function d Also copy functions , This will cause a problem of infinite recursion in passing values , So we need to add one &, To quote
If you pass the reference , Not as an output parameter , It is best to const & For protection ( That is, the internal member variables will not change )
notes : Pass values and parameters to classes in functions , And the return value is to call the copy construction
For the copy function automatically generated by the compiler , Do not distinguish between built-in types and custom types , Metropolitan processing
1、 Built in type , Shallow copy of byte order
2、 Custom type , Will call his copy construct
But because it is a shallow copy , For those who open up memory units by themselves ( As above Stack) It is difficult to realize the default copy structure , Will generate a crash
int main(){
Stack st1;
Stack st2(st1);
return 0;
}
The above code will crash , because st1 and st2 Generate space in the heap , First create st1, To create a st2, When destructor , First pair st2 Of _a To release space , turn st1 When , because _a Space has been released , Then there is no harm in the second release , So it will cause the program to crash
summary
| function | function |
|---|---|
| Constructors | Initialize object |
| Copy function | Copy objects |
| Destructor | Release the space opened up on the heap |
The above is the constructor , The difference between copy function and destructor
边栏推荐
- PHP exit codes description
- How should CDC be configured for Oracle cluster mode? I can run normally in stand-alone mode, but I can't read the increment in cluster mode
- Shell (12) regular expression
- 16、 Awk
- DNS
- SSH and NFS services
- 4.1 It is super simple to install QT without using Sogou Chinese input method
- Iptables firewall (I)
- 动态规划(背包问题)
- 23NFS共享存储服务
猜你喜欢
随机推荐
12、 Regular expression
Shell(13)三剑客
Shell
Shell script - automatically deploy DNS services
DNS
【无标题】
Shell (10) array and bubble sort
继承的详细介绍与理解,看了就懂
Create a daemon
LAMP.
[by pass] bypass method of WAF
13、 Command gadget
C language problem solving -- let the balloon rise
Linux部署MYSQL
EXPECT免交互
FTP service
Deveco could not resolve com.huawei.ohos:hap:2.4.5.0. error
Naive Bayes multiclass training model
MTCNN
c语言基础五子棋,十分的易懂理解,详细解释,容易上手









