当前位置:网站首页>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这种类的拷贝?说明它的实现是直接把内存中的值复制了一遍。指针的值是动态数组的内存地址。所以,复制指针的值是不对的。
边栏推荐
- 深入解析Kubebuilder
- JS variable case output user name
- Flex layout and usage
- Decorator basic learning 02
- ClickHouse(03)ClickHouse怎么安装和部署
- 深入解析Kubebuilder
- Some understandings about 01 backpacker
- Detect when a tab bar item is pressed
- Thread和Runnable创建线程的方式对比
- npm ERR! 400 Bad Request - PUT xxx - “devDependencies“ dep “xx“ is not a valid dependency name
猜你喜欢

基于Bevy游戏引擎和FPGA的双人游戏

Ansible overview and module explanation (you just passed today, but yesterday came to your face)

JDBC link Oracle reference code

A simple and beautiful regression table is produced in one line of code~

Mysql database (basic)

IMS data channel concept of 5g vonr+

Tree map: tree view - draw covid-19 array diagram

Introduction to namespace Basics
![[736. LISP syntax parsing]](/img/62/5e2aeec150096aa3fd81025d146255.png)
[736. LISP syntax parsing]
[email protected]映射关系问题"/>接口间调用为什么要用json、fastjson怎么赋值的、fastjson [email protected]映射关系问题
随机推荐
指针与数组在函数中输入实现逆序输出
Analysis -- MySQL statement execution process & MySQL architecture
U++4 接口 学习笔记
Chapter 9 Yunji datacanvas company won the highest honor of the "fifth digital finance innovation competition"!
Flex layout and usage
高数中值定理总结
JS variable case output user name
Read of shell internal value command
高手勿进!写给初中级程序员以及还在大学修炼的“准程序员”的成长秘籍
Error: No named parameter with the name ‘foregroundColor‘
ClickHouse(03)ClickHouse怎么安装和部署
批量归一化(标准化)处理
Detect when a tab bar item is pressed
Why do many people misunderstand technical debt
【数模】Matlab allcycles()函数的源代码(2021a之前版本没有)
Terms used in the Web3 community
What work items do programmers hate most in their daily work?
JS variable case
九章云极DataCanvas公司摘获「第五届数字金融创新大赛」最高荣誉!
Test interview | how much can you answer the real test interview question of an Internet company?