当前位置:网站首页>Dynamic creation and release, assignment and replication of objects
Dynamic creation and release, assignment and replication of objects
2022-04-23 06:37:00 【*Flowers bloom on the street】
One 、 Dynamic creation and release of objects
utilize new Operators can dynamically Allocate object space ,delete Operator Freeing object space .
The general form of dynamically allocating objects :
new Class name ;
use new The object dynamically allocated by the operator is nameless , It returns the value of a pointer to a new object , That is, the starting address of the allocated memory unit . The program can access this object indirectly through this address , Therefore, you need to define a pointer variable to the object of the class to store the address .
Class name * Object pointer variable ;
Object pointer variable = new Class name ;
Such as : student *p; p = new student;
In execution new Operation time , If there is not enough memory , Unable to open up the required memory space ,C++ The compiler will return a 0 Value pointer (NULL). therefore , Just check whether the return value is 0, You can judge whether the dynamic allocation object is successful , Object pointers can only be used when the pointer is valid .
When no longer needed, use by new When creating dynamic objects , Must use delete The operation is cancelled .
Such as : delete p;
Be careful :new Dynamic objects created are not automatically undone , Even if the program ends , It has to be used artificially delete revoke .
#include<iostream>
using namespace std;
class Object
{
private:
int value;
public:
Object(int x = 0) :value(x) {}
void Print()const { cout << value << endl; }
};
void fun()
{
Object obja(20);//.stack
}
int main()
{
Object objb(40);
fun();
Object* p = new Object(30);//new 1. To apply for space 2. Create objects 3. Return the address of the created object .heap
p->Print();
delete p;// When you don't need to delete
}
Object *p = new Object[n];// When building multiple objects at once , There must be a default constructor , There is no default constructor , Cannot continuously build objects .( If the constructor No parameters , Or constructor All parameters have default values , You can call it the default constructor . In a class , There can only be one default constructor .)

Two 、 Object assignment and replication
1> Assignment of objects
If a class defines two or more objects , Then these objects of the same kind can assign values to each other .
The general form of object assignment :
Object name 1= Object name 2;
Description of object assignment :
1) Only the data of non static members is assigned , Instead of assigning values to member functions .
2) The data member of the object cannot include the data of dynamically allocating resources , Otherwise, serious consequences may occur during assignment .
2> Replication of objects
Sometimes you need to use multiple identical objects , You can quickly copy multiple identical objects with an existing object .
The general form of object replication :
Class name Object name 2 ( Object name 1);
student stu2 (stu1) ;
Call a special constructor when creating an object — copy constructor . If the user Undefined copy constructor , Then the compiling system will Automatically provide a default copy constructor .
Such as : student stu2 (stu1) ;
Corresponding copy constructor :
student::student(const student &stu)
{
num=stu.num;
name=stu.name;
score =stu.score;
}
#include<stdio.h>
#include<iostream>
using namespace std;
class student
{
private:
int num;
string name;
float score;
public:
student(int n, string m, float s) :num(n), name(m), score(s){};
student(const student& stu);
void display()
{
cout << "num:" << num << endl;
cout << "name:" << name << endl;
cout << "score:" << score << endl;
}
};
student::student(const student&stu)// copy constructor
{
num = stu.num;
name = stu.name;
score = stu.score;
}
int main()
{
student stu1(2020, "zhangsan", 85);
student stu2(stu1);// Copy
stu1.display();
stu2.display();
return 0;
}
C++ It also provides another user-friendly copy form —— Replace parentheses with assignment numbers .
The general form of object replication :
Class name Object name 2 = Object name 1;
The difference between the assignment of an object and the copy of an object :
1) The assignment of an object is to assign a value to an existing object , The assigned object must be defined first , Can be assigned .
2) If the copy of an object is to create a new object , And make it exactly the same as an existing object .
The difference between ordinary constructor and copy constructor :
1) The declaration is different in form .
Class name ( Parameter list )
Class name ( Class name & Object name )
Such as : student( int num, string name, int score);
student (student &stu);
2) When creating an object , Argument types are different .
Such as : student stu1( 20060102,“ Zhang San ”,86 );
student stu2( stu1);
3) Ordinary constructors are called when creating objects .
The copy constructor is called when copying a new object from an existing object .
版权声明
本文为[*Flowers bloom on the street]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230547207094.html
边栏推荐
猜你喜欢

从源代码到可执行文件的过程
![[untitled] database - limit the number of returned rows](/img/20/9a143e6972f1ce2eed5a3d11c3a46d.png)
[untitled] database - limit the number of returned rows

Understanding and installing MySQL

MySQL table constraints and table design

大学概率论与数理统计知识点详细整理

Explanation of login page

Generation of verification code

渔网道路密度计算

Robocode教程7——雷达锁定

Detailed arrangement of knowledge points of University probability theory and mathematical statistics
随机推荐
【无标题】
ThreadLocal. Threadlocalmap analysis
[leetcode 54] spiral matrix
Export of data
MySQL advanced query
破解滑动验证码
Storing inherited knowledge in cloud computing
C#中?的这种形式
爬取蝉妈妈数据平台商品数据
用C语言实现重写strcmp等四个函数
Addition, deletion, modification and query of MySQL table
Rust 中的 Rc智能指针
MySQL occasional Caton
Conversion between JS object and string
Motor and drive (Qi Jinqing Edition)
安装pyshp库
Robocode教程8——AdvancedRobot
Qthread simple test understanding
【UDS统一诊断服务】四、诊断典型服务(3)— 读故障信息功能单元(存储数据传输功能单元)
Explanation of login page
