I have a problem very similar to
How to make a class with a member of unique_ptr work with std::move and std::swap?
and I am using VS2013 so I expect the following to work.
#include <memory>
#include <string>
class AbstractCamera{
public:
AbstractCamera(const std::string& name) :_name(name){}
virtual void method() = 0;
std::string _name;
};
class CameraImpl : public AbstractCamera{
public:
CameraImpl() :AbstractCamera("CameraImpl"){}
void method(){}
};
class RenderManager{
public:
RenderManager():_currentCamera(std::move(std::make_unique<CameraImpl>())){}
private:
std::unique_ptr<AbstractCamera> _currentCamera;
};
class Engine{
public:
Engine(){}
private:
RenderManager r;
};
int main(){
Engine e;
e = Engine(); // Causes error: C2280 call of deleted function
}
path\engine.cpp(75): error C2280: 'std::unique_ptr> &std::unique_ptr<_Ty,std::default_delete<_Ty>>::operator =(const std::unique_ptr<_Ty,std::default_delete<_Ty>> &)' : attempting to reference a deleted function with [ _Ty=AbstractCamera ] other_path\memory(1487) : see declaration of 'std::unique_ptr>::operator =' with [ _Ty=AbstractCamera ] This diagnostic occurred in the compiler generated function 'RenderManager &RenderManager::operator =(const RenderManager &)'
I do get that simple examples like
std::unique_ptr<A> a = std::make_unique<A>();
std::unique_ptr<A> b = a;
are not allowed but my problem is the following code:
Engine e;
e = Engine();
because the assignment operator of unique_ptr is deleted but how does this affect a class hierarchy like those of Engine->RenderManger->unique_ptr member.
I do get, that Engine e
uses the default constructor Engine() and e = Engine()
calls the default constructor and the operator= of Engine to assign the temporary Engine object to e.
My question therefore is: where does the code try to copy/assign unique_ptr and how do I resolve it?
I tried to strip the original code down as much as possible but was not able to reproduce the error using a simpler example and ideone in form of a SSCCEE as I do not really understand what causes the problem, so sorry for that!