当前位置:网站首页>vector和类拷贝构造函数
vector和类拷贝构造函数
2022-07-06 22:46:00 【novanova2009】
// vec_d.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//
#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添加元素的时候采用的是拷贝构造函数。编译器生成的是浅拷贝的拷贝构造函数。
test1这个类采用了固定长度的数组,浅拷贝没有问题。
test这个类采用了指针,实现动态长度数组。需要使用深拷贝构造函数。
默认的拷贝构造函数为什么能够正确处理test1这种类的拷贝?说明它的实现是直接把内存中的值复制了一遍。指针的值是动态数组的内存地址。所以,复制指针的值是不对的。
边栏推荐
- Gavin teacher's perception of transformer live class - rasa project actual combat e-commerce retail customer service intelligent business dialogue robot microservice code analysis and dialogue experim
- 动态生成表格
- 高手勿进!写给初中级程序员以及还在大学修炼的“准程序员”的成长秘籍
- [736. LISP syntax parsing]
- Vscode 如何使用内置浏览器?
- AttributeError: module ‘torch._C‘ has no attribute ‘_cuda_setDevice‘
- R语言主成分pca、因子分析、聚类对地区经济研究分析重庆市经济指标
- Thread和Runnable创建线程的方式对比
- Test interview | how much can you answer the real test interview question of an Internet company?
- LabVIEW在打开一个新的引用,提示内存已满
猜你喜欢

Inventory host list in ansible (I wish you countless flowers and romance)

Chapter 9 Yunji datacanvas company has been ranked top 3 in China's machine learning platform market

offer如何选择该考虑哪些因素

01 machine learning related regulations

mpf2_ Linear programming_ CAPM_ sharpe_ Arbitrage Pricin_ Inversion Gauss Jordan_ Statsmodel_ Pulp_ pLU_ Cholesky_ QR_ Jacobi

Introduction to the PureMVC series

Oracle - views and sequences

Vscode 如何使用内置浏览器?

Vscode automatically adds a semicolon and jumps to the next line

Gavin teacher's perception of transformer live class - rasa project actual combat e-commerce retail customer service intelligent business dialogue robot microservice code analysis and dialogue experim
随机推荐
全国气象数据/降雨量分布数据/太阳辐射数据/NPP净初级生产力数据/植被覆盖度数据
Thesis landing strategy | how to get started quickly in academic thesis writing
Function pointer and pointer function in C language
Why do many people misunderstand technical debt
JS variable plus
装饰器基础学习02
NiO related knowledge points (I)
如何设计 API 接口,实现统一格式返回?
DFS and BFS concepts and practices +acwing 842 arranged numbers (DFS) +acwing 844 Maze walking (BFS)
JS variable
史上最全学习率调整策略lr_scheduler
【736. Lisp 语法解析】
批量归一化(标准化)处理
STM32F103实现IAP在线升级应用程序
Error: No named parameter with the name ‘foregroundColor‘
3.基金的类型
[ArcGIS tutorial] thematic map production - population density distribution map - population density analysis
Ansible中的inventory主机清单(预祝你我有数不尽的鲜花和浪漫)
Depth first traversal template principle of tree and graph
01 machine learning related regulations