Here I have tried to use unique_ptr in constructor. It gives the following error:
function "std::unique_ptr<_Ty, _Dx>::operator=(const std::unique_ptr<_Ty, _Dx>::_Myt &) [with _Ty=ABC, _Dx=std::default_delete]" (declared at line 1487 of "C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\include\memory") cannot be referenced -- it is a deleted function
How can I achieve it?
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)- Who is going to own the pointer?n(std::move(n1)).