当前位置:网站首页>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 .
边栏推荐
- The founder has a debt of 1billion. Let's start the class. Is it about to "end the class"?
- 想要选择一些部门优先使用 OKR, 应该如何选择试点部门?
- [optimal web page width and its implementation] [recommended collection "
- 3. Type of fund
- 10 distributed databases that take you to the galaxy
- DFS, BFS and traversal search of Graphs
- ASP. Net MVC - resource cannot be found error - asp Net MVC – Resource Cannot be found error
- What changes will PMP certification bring?
- AOSP ~Binder 通信原理 (一) - 概要
- JHOK-ZBG2漏电继电器
猜你喜欢
《二》标签
利用OPNET进行网络指定源组播(SSM)仿真的设计、配置及注意点
pytest测试框架——数据驱动
Error: No named parameter with the name ‘foregroundColor‘
DOM-节点对象+时间节点 综合案例
QT simple layout box model with spring
torch optimizer小解析
Analysis -- MySQL statement execution process & MySQL architecture
c语言神经网络基本代码大全及其含义
利用OPNET进行网络仿真时网络层协议(以QoS为例)的使用、配置及注意点
随机推荐
The execution order of return in JS' try catch finally
No experts! Growth secrets for junior and intermediate programmers and "quasi programmers" who are still practicing in Universities
Clickhouse (03) how to install and deploy Clickhouse
Why JSON is used for calls between interfaces, how fastjson is assigned, fastjson 1.2 [email protected] Mapping relatio
Development thoughts of adding new requirements in secondary development
Dynamically generate tables
ThinkPHP关联预载入with
DBSync新增对MongoDB、ES的支持
在米家、欧瑞博、苹果HomeKit趋势下,智汀如何从中脱颖而出?
Dbsync adds support for mongodb and ES
Leetcode (46) - Full Permutation
NPDP产品经理认证,到底是何方神圣?
2039: [蓝桥杯2022初赛] 李白打酒加强版 (动态规划)
Timer create timer
LabVIEW在打开一个新的引用,提示内存已满
Understand common network i/o models
[QT] custom control loading
Weebly mobile website editor mobile browsing New Era
高数中值定理总结
window定时计划任务