在回答这个问题时,我被告知我的问题在于调用下面代码中的复制构造函数.但是,我只是看不到它被调用的位置.我没有做任何事情,agents[1] = agents[0];虽然显然我不明白.这种复制发生在哪里?如何更改它,以便每次只创建新对象?
我已经
int main()
{
Level* level;
std::vector<Agent> agents;
level = new Level(agents);
for (int i = 0; i < 1; i++) // this will be more than 1 in the future.
{
agents.push_back(Agent(100, *level, agents, level->Pickups(), D3DXCOLOR(1.0F, 0.4f, 0.4f, 1.0f)));
}
delete level;
}
Run Code Online (Sandbox Code Playgroud) 在我的代码中有 operator+ 重载。在这个范围内,我定义了 object ans,我想构建并返回它,但似乎析构函数ans在我可以返回它之前就进行了破坏,所以这个方法返回了一些未定义的值。
我不明白我错在哪里?它是析构函数、构建器还是在我的 operator+ 重载中?
这是我的代码:
class vector1{
int * arr;
int size;
public:
//constructors
vector1(){}
vector1(int n){
size=n;
arr= new int[size];
}
//functions
int get_size(){return size;}
void init(){ //initialize all array cells to 0
for(int i=0;i<size;i++)
arr[i]=0;
}
int get_value_in_index(int index){
return arr[index];
}
void set_value_in_index(int i, int num){
arr[i]=num;
}
int & operator[](int i){
int default_val=0;
if (i<0 || i>size){
cout<<"index not valid"<<endl;
return default_val;
}
return arr[i];
}
vector1 operator+(vector1 & …Run Code Online (Sandbox Code Playgroud) 我的问题是最后的陈述,即之前的陈述 return 0;
当我们尝试将int值分配给对象时,为什么要调用参数化构造函数.
我的代码:
#include<iostream>
using namespace std;
class Test {
private:
int i;
public:
Test(int s=0):i(s) {
cout<<"param ctor: "<<i<<endl;
}
};
int main()
{
Test a; //param ctor called
Test b(5); //param ctor called
//b(a); //error as we have not implemented copy ctor
b=a; //compiler provided assignment opr. called
b=100; //why param ctor called for this.??
return 0;
}
Run Code Online (Sandbox Code Playgroud)
OUTPUT:
param ctor: 0
param ctor: 5
param ctor: 100
Run Code Online (Sandbox Code Playgroud) 我用memcpy复制一个Vertex由glm :: vec3对象组成的结构.它可以在类函数中复制结构.它在该函数返回类对象时调用的复制构造函数中不起作用.
为什么?
类函数返回对象
ShapeData ShapeGenerator::drawTriangle() {
ShapeData ret;
Vertex verts[] = {
glm::vec3(0.0f, 1.0f, 0.0f),
glm::vec3(1.0f, 0.0f, 0.0f),
glm::vec3(-1.0f, -1.0f, 0.0f),
glm::vec3(0.0f, 1.0f, 0.0f),
glm::vec3(1.0f, -1.0f, 0.0f),
glm::vec3(0.0f, 0.0f, 1.0f),
};
ret.numVerts = NUM_ARRAY_ELEMENTS(verts);
ret.verts = new Vertex[ret.numVerts];
memcpy(ret.verts, verts, sizeof(verts)); //WORKS
GLushort indicies[] = {0,1,2};
ret.numIndicies = NUM_ARRAY_ELEMENTS(indicies);
ret.indicies = new GLushort[ret.numIndicies];
memcpy(ret.indicies, indicies, sizeof(indicies));
return ret;
}
Run Code Online (Sandbox Code Playgroud)
复制构造函数
ShapeData(const ShapeData& data) {
verts = new Vertex[data.numVerts];
//memcpy(verts, data.verts, sizeof(data.verts)); //DOES NOT WORK
std::copy( data.verts, …Run Code Online (Sandbox Code Playgroud) 我知道 atomic 有一个已删除的复制构造函数,但是我该怎么做才能使此代码正常工作?我怎么可能在 vector 中为 atomic 定义一个复制构造函数?
#include <atomic>
#include <vector>
int main() {
std::vector<std::atomic<int>> examp;
examp.resize(64);
}
Run Code Online (Sandbox Code Playgroud) 码:
class A
{
public:
A()
{
cout<<"Defualt Constructor"<<endl;
}
A(A &t)
{
cout<<"Copy Constructor"<<endl;
}
};
A func()
{
cout<<"In func"<<endl;
}
int main()
{
A a1;
A a2;
a2 = func();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
该程序工作正常.另外,如果我调用这样的函数:
A a2 = func();
Run Code Online (Sandbox Code Playgroud)
并const在复制 构造函数参数中添加限定符,如:
A(const A &t)
{
cout<<"Copy Constructor"<<endl;
}
Run Code Online (Sandbox Code Playgroud)
还有,工作正常.
但是,如果const从复制构造函数参数中删除如:
A(A &t)
{
cout<<"Copy Constructor"<<endl;
}
Run Code Online (Sandbox Code Playgroud)
并调用函数 func()
A a2 = func();
Run Code Online (Sandbox Code Playgroud)
编译器给出错误:
error: invalid initialization of non-const reference of …Run Code Online (Sandbox Code Playgroud) 我在单个 cpp 文件中有以下代码:
class Base
{
public:
//constructor
Base() = delete;
};
class Derived : public Base
{
public:
//copy constructor
Derived( const Derived & other ){};
};
int main( int argc, char* argv[] )
{
//empty
}
Run Code Online (Sandbox Code Playgroud)
但是编译cpp文件会报错
exp.cpp:在复制构造函数'Derived::Derived(const Derived&)'中:
exp.cpp:15:37: 错误:使用已删除的函数'Base::Base()'
Derived(const Derived & other){};
exp.cpp:7:5: 注意:这里声明
Base() = delete;
^~~~
我不明白为什么。当您为派生类定义复制构造函数时,基类默认构造函数如何发挥作用?
为什么我们需要复制构造函数?
我正在学习 C++,但我无法理解复制构造函数的必要性,因为也没有使用复制构造函数,我得到了正确的输出。我经历了几个例子,但在我看来,拥有复制构造函数只是一个好习惯,比如初始化变量。有人可以帮助我理解复制构造函数的概念。任何帮助将不胜感激。谢谢你。
我知道 C++ 移动语义应该节省处理器能力和内存,因为
Move::Move(Shallow&& source) noexcept // Move constructor
{
data = source.data; // Assume data is an array of size = size and all initiated to a user specific value
size = source.size;
source.size =0;
source.data ={nullptr};
}
Run Code Online (Sandbox Code Playgroud)
假设所有数组索引都被初始化为一个特定的变量,这样移动语义只会将数组指针保存在内存中,并将源数组清空,就像上面的例子一样,这将阻止动态创建新数组,如果我们使用复制构造函数(特定的深复制构造函数)但是
1) 如果我们假设数据只是一个简单的整数,甚至是一个非常大的未初始化数组,那么使用移动构造函数是否有任何好处 2) 这个移动构造函数看起来与仅使用浅复制调用复制构造函数非常相似,例如
Copy::Copy(const &source){ // A shallow copy
data = source.data; // Assume data is an array of size = size
size = source.size;
}
Run Code Online (Sandbox Code Playgroud)
唯一的区别当然是清空数据和移动构造中的大小,因此在内存和性能方面,上面两段代码之间是否有任何性能改进,因为清空数据指针和大小实际上并没有节省我们的空间或内存对,或者我想念这里的东西。
这个问题使我可以知道何时使用浅复制或移动语义,以及它们之间是否有任何区别(除了以移动方式将属性归零)。
我有以下代码:
#include <bits/stdc++.h>
using namespace std;
class A {
public:
A(const A& a) noexcept { cout << "copy constructor" << endl; }
A& operator=(const A& a) noexcept { cout << "copy assignment operator" << endl; }
A(A&& a) noexcept { cout << "move constructor" << endl; }
A& operator=(A&& a) noexcept { cout << "move assignment operator" << endl; }
A() { cout << "default constructor" << endl; }
};
vector<A> aList;
void AddData(const A&& a)
{
aList.push_back(std::move(a));
}
int …Run Code Online (Sandbox Code Playgroud) c++ ×10
copy-constructor ×10
c++11 ×2
arrays ×1
atomic ×1
c++14 ×1
constructor ×1
memcpy ×1
move ×1
reference ×1
shallow-copy ×1
vector ×1