当前位置:网站首页>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这种类的拷贝?说明它的实现是直接把内存中的值复制了一遍。指针的值是动态数组的内存地址。所以,复制指针的值是不对的。
边栏推荐
- Flex layout and usage
- IMS data channel concept of 5g vonr+
- [digital analog] source code of MATLAB allcycles() function (not available before 2021a)
- PLC Analog output analog output FB analog2nda (Mitsubishi FX3U)
- Weebly移动端网站编辑器 手机浏览新时代
- JS variable plus
- U++4 接口 学习笔记
- STM32 system timer flashing LED
- Appium practice | make the test faster, more stable and more reliable (I): slice test
- acwing 843. N-queen problem
猜你喜欢
Flask项目使用flask-socketio异常:TypeError: function() argument 1 must be code, not str
Weebly移动端网站编辑器 手机浏览新时代
DFS和BFS概念及实践+acwing 842 排列数字(dfs) +acwing 844. 走迷宫(bfs)
Camera calibration (I): robot hand eye calibration
Oracle - views and sequences
Function pointer and pointer function in C language
U++ 元数据说明符 学习笔记
Common Oracle SQL statements
Liste des hôtes d'inventaire dans ansible (je vous souhaite des fleurs et de la romance sans fin)
为什么很多人对技术债务产生误解
随机推荐
JS variable case output user name
Can I specify a path in an attribute to map a property in my class to a child property in my JSON?
Vscode 如何使用内置浏览器?
sublime使用技巧
Analyse approfondie de kubebuilder
Thread和Runnable创建线程的方式对比
What is JVM? What are the purposes of JVM tuning?
JS input and output
What is Web3
JS variable case
Markdown编辑器
Detect when a tab bar item is pressed
Techniques d'utilisation de sublime
How does vscade use the built-in browser?
LabVIEW在打开一个新的引用,提示内存已满
A simple and beautiful regression table is produced in one line of code~
ClickHouse(03)ClickHouse怎么安装和部署
In depth analysis of kubebuilder
U++4 接口 学习笔记
Two methods of chromosome coordinate sequencing