当前位置:网站首页>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 .
边栏推荐
- CentOS 7.9安装Oracle 21c历险记
- Creation and use of thread pool
- Pytest testing framework -- data driven
- Is it necessary to renew the PMP certificate?
- Leetcode(46)——全排列
- pmp真的有用吗?
- If you‘re running pod install manually, make sure flutter pub get is executed first.
- QT控件样式系列(一)之QSlider
- 漏电继电器JOLX-GS62零序孔径Φ100
- No experts! Growth secrets for junior and intermediate programmers and "quasi programmers" who are still practicing in Universities
猜你喜欢
随机推荐
最长不下降子序列(LIS)(动态规划)
高手勿进!写给初中级程序员以及还在大学修炼的“准程序员”的成长秘籍
一个酷酷的“幽灵”控制台工具
数字化如何影响工作流程自动化
Timer create timer
pmp真的有用吗?
DOM-节点对象+时间节点 综合案例
《二》标签
线程池的创建与使用
背包问题(01背包,完全背包,动态规划)
Disk monitoring related commands
Longest palindrome substring (dynamic programming)
Why JSON is used for calls between interfaces, how fastjson is assigned, fastjson 1.2 [email protected] Mapping relatio
How can professional people find background music materials when doing we media video clips?
Simulate thread communication
How does redis implement multiple zones?
Harmonyos fourth training
Understand common network i/o models
【QT】自定义控件-Loading
Linkedblockingqueue source code analysis - initialization









