当前位置:网站首页>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 .
边栏推荐
猜你喜欢
随机推荐
Salesforce 容器化 ISV 场景下的软件供应链安全落地实践
How can project managers counter attack with NPDP certificates? Look here
[opencv] image morphological operation opencv marks the positions of different connected domains
JVM(二十) -- 性能监控与调优(一) -- 概述
【js组件】date日期显示。
基于Bevy游戏引擎和FPGA的双人游戏
ASP. Net MVC - resource cannot be found error - asp Net MVC – Resource Cannot be found error
模拟线程通信
pytest测试框架——数据驱动
Timer create timer
想要选择一些部门优先使用 OKR, 应该如何选择试点部门?
App embedded H5 --- iPhone soft keyboard blocks input text
【opencv】图像形态学操作-opencv标记不同连通域的位置
10 distributed databases that take you to the galaxy
ThinkPHP关联预载入with
Linkedblockingqueue source code analysis - initialization
NPDP产品经理认证,到底是何方神圣?
LinkedBlockingQueue源码分析-初始化
DOM-节点对象+时间节点 综合案例
Torch optimizer small parsing









