当前位置:网站首页>Vector and class copy constructors
Vector and class copy constructors
2022-07-07 05:20:00 【novanova2009】
// vec_d.cpp : This file contains "main" function . Program execution will start and end here .
//
#include <iostream>
#include <vector>
class test
{
public:
double *a;
test() { a = nullptr; };
test(int size) {
a = new double[size];
for (int i=0;i<size;i++)
{
a[i] = i;
}
};
};
class test1
{
public:
test1() {};
test1(double val) {
for (int i = 0; i < 10; i++)
{
a[i] = val;
}
};
private:
double a[10];
};
using namespace std;
int main()
{
vector<test1> vec_t;
test1 new_t(10.0);
vec_t.push_back(new_t);
}
vector The copy constructor is used when adding elements . The compiler generates a shallow copy of the copy constructor .
test1 This class uses a fixed length array , Shallow copy is OK .
test This class uses pointers , Realize dynamic length array . You need to use a deep copy constructor .
Why can the default copy constructor handle correctly test1 This kind of copy ? It shows that its implementation is to directly copy the values in memory . The value of the pointer is the memory address of the dynamic array . therefore , It is wrong to copy the value of the pointer .
边栏推荐
- How can professional people find background music materials when doing we media video clips?
- AOSP ~Binder 通信原理 (一) - 概要
- vector和类拷贝构造函数
- Error: No named parameter with the name ‘foregroundColor‘
- DFS,BFS以及图的遍历搜索
- Leetcode minimum difference in student scores
- y58.第三章 Kubernetes从入门到精通 -- 持续集成与部署(三一)
- LinkedBlockingQueue源码分析-初始化
- Safe landing practice of software supply chain under salesforce containerized ISV scenario
- 一个酷酷的“幽灵”控制台工具
猜你喜欢
随机推荐
Timer create timer
If you‘re running pod install manually, make sure flutter pub get is executed first.
2039: [Bluebridge cup 2022 preliminaries] Li Bai's enhanced version (dynamic planning)
pmp真的有用吗?
3. Type of fund
最长公共子序列(LCS)(动态规划,递归)
[optimal web page width and its implementation] [recommended collection "
U++4 interface learning notes
HarmonyOS第四次培训
Disk monitoring related commands
Why JSON is used for calls between interfaces, how fastjson is assigned, fastjson 1.2 [email protected] Mapping relatio
Phenomenon analysis when Autowired annotation is used for list
Error: No named parameter with the name ‘foregroundColor‘
The founder has a debt of 1billion. Let's start the class. Is it about to "end the class"?
What changes will PMP certification bring?
Longest common subsequence (LCS) (dynamic programming, recursive)
JHOK-ZBL1漏电继电器
《四》表单
记录一次压测经验总结
JHOK-ZBG2漏电继电器