Search code examples
c++object-initializers

how to initialize a non copyable object data member


I want to use an already implemented abstract class (which can be seen as "A" in my simple provided code) in my code. I defined class "B" to implement those pure virtual methods. The fact, here, is that the objects of this class cannot be copyable then, as the operator= is deleted in abstract class. I have class "Z" which has a data member of "B" class object. I want to initialize the object as you see in the code. But as it is a non-copyable one, for sure it show errors like use of deleted function ‘NS::B::B(NS::B&&)’ . I don't know how should I have this object as data member and also initialize it with proper data. The simple version of the code is as follow:

#include <string>
#include <iostream>
#include <memory>

namespace NS {
class A //abstract class
{
public:
    A() = default;
    A& operator=(const A& other) = delete;
    A(const A& other) = delete;
};
}

namespace NS {
class B : public A
{
public:
    B(int p);
};
}

namespace SI {
class Z
{
public:
    Z(int p, std::string name);
private:
    NS::B obj3;
};
typedef std::shared_ptr<SI::Z> ZPtr;
}

SI::Z::Z(int p, std::string name) : obj3(p)
{}

namespace SI {
class Y
{
public:
    Y(int p);
private:
    SI::ZPtr obj2;
};
}

SI::Y::Y(int p) : obj2(std::make_shared<SI::Z>(SI::Z(p,"hi")))
{}


Solution

  • To make the above compile:

    Add the headers:

    #include <string>
    #include <memory>
    

    You also need to be able to construct the A so you need to make the constructors public:

    class A //abstract class
    {
        public:               // Added this:
             A() = default;
        .....
    };
    

    Your main issue is here in making the shared object.

     obj2(std::make_shared<SI::Z>(SI::Z(p,"hi")))
    

    You don't need to constructed the SI::Z object here (as it is not copyable this is an issue). What you want to do is pass the arguments that will be used to create the SI::Z object. Then std::make_shared() will call new and forward these parameters to the constructor.

     obj2(std::make_shared<SI::Z>(p, "hi"))     // Notice the diff?