lundi 19 novembre 2018

Avoid calling default, move and copy constructor

I have following example (extension to Avoid calling move constructor)

#include <cstdint>

class Interface
{
public:
   Interface() = default;
   virtual ~Interface() = default;
   Interface(const Interface&) = delete;
   Interface(Interface&&) = delete;
   const Interface& operator=(const Interface&) = delete;
   Interface& operator=(Interface&&) = delete;
};

class FooC : public Interface
{
public:
   FooC(uint16_t iPort, uint16_t iPin)
   : PORT(iPort)
   , PIN(iPin)
   {
   };

   FooC() = delete;
   ~FooC() override = default;
   FooC(const FooC&) = delete;
   FooC(FooC&&) = delete;
   const FooC& operator=(const FooC&) = delete;
   FooC& operator=(FooC&&) = delete;

private:
   const uint16_t PORT;
   const uint16_t PIN;
};

class FactoryC
{
public:
   FactoryC()
   : mFoo{
     {1, 2},
     {3, 4}
   }
   {
   };

   ~FactoryC() = default;
   FactoryC(const FactoryC&) = delete;
   FactoryC(FactoryC&&) = delete;
   const FactoryC& operator=(const FactoryC&) = delete;
   FactoryC& operator=(FactoryC&&) = delete;

private:
   FooC mFoo[2];
};

int main()
{
    FactoryC factory{};
}

and I don't want to call the default, move and copy constructor. Due to that I deleted the functions. Unfortunately this results in following error (compiled with C++17)

<source>: In constructor 'FactoryC::FactoryC()':

<source>:42:4: error: use of deleted function 'FooC::FooC(FooC&&)'

    }

    ^

<source>:26:4: note: declared here

    FooC(FooC&&) = delete;

    ^~~~

Compiler returned: 1

Is it possible to force in this example the calling of constructor with the parameters and still delete the default, move and copy constructor of FooC?

Aucun commentaire:

Enregistrer un commentaire