【发布时间】:2016-03-18 10:58:20
【问题描述】:
这里我尝试在构造函数中使用 unique_ptr。它给出了以下错误:
函数“std::unique_ptr<_ty _dx>::operator=(const std::unique_ptr<_ty _dx>::_Myt &) [with _Ty=ABC, _Dx=std::default_delete]”(声明在“C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\include\memory”的第 1487 行)不能被引用——它是一个被删除的函数
我怎样才能实现它?
StructCol.h
#include "stdafx.h"
#ifndef StructCol_H
#define StructCol_H
#include<string>
#include<memory>
using namespace std;
class ABCD
{
public:
std::unique_ptr<ABC> & n;
ABCD(std::unique_ptr<ABC> & n1) : n(n1)
{
n = n1;
}
void print()
{
cout << n->no << endl;
cout << n->text_c << endl;
cout << n->no_c << endl;
}
};
class ABC
{
public:
string test;
int no;
string text_c;
int no_c;
ABC()
{
}
ABC(string text_c1, int no_c1)
{
text_c = text_c1;
no_c = no_c1;
}
void print()
{
cout << test << endl;
cout << no << endl;
cout << text_c << endl;
cout << no_c << endl;
}
};
#endif
【问题讨论】:
-
删除
n = n1; -
ABCD(std::unique_ptr<ABC> & n1) : n(n1)- 谁将拥有指针? -
更糟糕的是,您引用的是 unique_ptr,而不是复制
-
为什么人们不赞成这个问题?
-
必须是
n(std::move(n1))。
标签: c++