当前位置:网站首页>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 .
边栏推荐
- 10 distributed databases that take you to the galaxy
- App embedded H5 --- iPhone soft keyboard blocks input text
- 批量归一化(标准化)处理
- Longest common subsequence (LCS) (dynamic programming, recursive)
- Creation and use of thread pool
- Longest non descent subsequence (LIS) (dynamic programming)
- 第一篇论文的写作流程
- 3. Type of fund
- HarmonyOS第四次培训
- 利用OPNET进行网络单播(一服务器多客户端)仿真的设计、配置及注意点
猜你喜欢
Complete code of C language neural network and its meaning
DOM-节点对象+时间节点 综合案例
【js组件】date日期显示。
EGR-20USCM接地故障继电器
y58.第三章 Kubernetes从入门到精通 -- 持续集成与部署(三一)
Leetcode(417)——太平洋大西洋水流问题
Analysis -- MySQL statement execution process & MySQL architecture
【js组件】自定义select
Error: No named parameter with the name ‘foregroundColor‘
Is it necessary to renew the PMP certificate?
随机推荐
How can project managers counter attack with NPDP certificates? Look here
The sooner you understand the four rules of life, the more blessed you will be
批量归一化(标准化)处理
DFS, BFS and traversal search of Graphs
What changes will PMP certification bring?
Longest non descent subsequence (LIS) (dynamic programming)
JVM(二十) -- 性能监控与调优(一) -- 概述
漏电继电器JELR-250FG
Scheduledexecutorservice timer
np. random. Shuffle and np Use swapaxis or transfer with caution
vector和类拷贝构造函数
JS 的 try catch finally 中 return 的执行顺序
利用OPNET进行网络单播(一服务器多客户端)仿真的设计、配置及注意点
《四》表单
Knapsack problem unrelated to profit (depth first search)
Addressable 预下载
DOM-节点对象+时间节点 综合案例
拿到PMP认证带来什么改变?
K6EL-100漏电继电器
高手勿进!写给初中级程序员以及还在大学修炼的“准程序员”的成长秘籍