当前位置:网站首页>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 .
边栏推荐
- window定时计划任务
- 漏电继电器JD1-100
- Scheduledexecutorservice timer
- Error: No named parameter with the name ‘foregroundColor‘
- DFS,BFS以及图的遍历搜索
- Longest non descent subsequence (LIS) (dynamic programming)
- 《二》标签
- Operand of null-aware operation ‘!‘ has type ‘SchedulerBinding‘ which excludes null.
- Two methods of thread synchronization
- torch optimizer小解析
猜你喜欢
随机推荐
ThinkPHP关联预载入with
pmp真的有用吗?
Tencent cloud database public cloud market ranks top 2!
K6EL-100漏电继电器
Weebly移动端网站编辑器 手机浏览新时代
2. Overview of securities investment funds
Knapsack problem (01 knapsack, complete knapsack, dynamic programming)
ASP. Net MVC - resource cannot be found error - asp Net MVC – Resource Cannot be found error
JS 的 try catch finally 中 return 的执行顺序
App embedded H5 --- iPhone soft keyboard blocks input text
Pytest testing framework -- data driven
sublime使用技巧
AOSP ~Binder 通信原理 (一) - 概要
高数中值定理总结
JHOK-ZBL1漏电继电器
U++4 interface learning notes
How does redis implement multiple zones?
DBSync新增对MongoDB、ES的支持
Disk monitoring related commands
No experts! Growth secrets for junior and intermediate programmers and "quasi programmers" who are still practicing in Universities









