当前位置:网站首页>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这种类的拷贝?说明它的实现是直接把内存中的值复制了一遍。指针的值是动态数组的内存地址。所以,复制指针的值是不对的。
边栏推荐
- MySQL数据库(基础篇)
- Tiktok may launch an independent grass planting community platform: will it become the second little red book
- Canteen user dish relationship system (C language course design)
- Read of shell internal value command
- 深入解析Kubebuilder
- 装饰器基础学习02
- Ansible中的inventory主機清單(預祝你我有數不盡的鮮花和浪漫)
- [Android kotlin collaboration] use coroutinecontext to realize the retry logic after a network request fails
- 01 machine learning related regulations
- 接口间调用为什么要用json、fastjson怎么赋值的、fastjson [email protected]映射关系问题
猜你喜欢
Flask项目使用flask-socketio异常:TypeError: function() argument 1 must be code, not str
Introduction to the PureMVC series
How does vscade use the built-in browser?
Analysis -- MySQL statement execution process & MySQL architecture
Ansible overview and module explanation (you just passed today, but yesterday came to your face)
[hand torn STL] list
JS also exports Excel
一文搞懂常见的网络I/O模型
[line segment tree practice] recent requests + area and retrieval - array modifiable + my schedule I / III
In depth analysis of kubebuilder
随机推荐
How to design API interface and realize unified format return?
Flask项目使用flask-socketio异常:TypeError: function() argument 1 must be code, not str
Talk about the importance of making it clear
IMS data channel concept of 5g vonr+
5G VoNR+之IMS Data Channel概念
深入解析Kubebuilder
Chapter 9 Yunji datacanvas company has been ranked top 3 in China's machine learning platform market
System framework of PureMVC
Canteen user dish relationship system (C language course design)
JS 的 try catch finally 中 return 的执行顺序
Can I specify a path in an attribute to map a property in my class to a child property in my JSON?
U++ 游戏类 学习笔记
A line of R code draws the population pyramid
Ansible reports an error: "MSG": "invalid/incorrect password: permission denied, please try again“
Jetson nano configures pytorch deep learning environment / / to be improved
R language principal component PCA, factor analysis, clustering analysis of regional economy analysis of Chongqing Economic Indicators
JS also exports Excel
Monitoring cannot be started after Oracle modifies the computer name
关于01背包个人的一些理解
第一篇论文的写作流程