How do I create new and assign a value to a private unique_ptr in the constructor of a class? Tyvm :^) Keith
My best effort:
#include <iostream>
#include <memory>
class A {
public:
A() {};
A(int);
void print();
private:
std::unique_ptr<int> int_ptr_;
};
A::A(int a) {
int_ptr_ = new int(a);
}
void A::print() {
std::cout << *int_ptr_ << std::endl;
}
int main() {
A a(10);
a.print();
std::cout << std::endl;
}
Compiler result:
smartPointer2.1.cpp:13:11: error: no match for ‘operator=’ (operand types are ‘std::unique_ptr<int>’ and ‘int*’)
int_ptr_ = new int(a);