I am constructing derived class from non-copyable base class. I would like to aggregate-initialize Base
in the initializer:
// for convenience, could be any other way to disable copy
#include<boost/noncopyable.hpp>
struct Base: public boost::noncopyable{
int a;
};
struct Derived: public Base{
Derived(int a): Base{a} {}
};
but I am getting:
error: could not convert ‘a’ from ‘int’ to ‘boost::noncopyable_::noncopyable’
As I understand, noncopyable
cannot be initialized, fair enough. Can I then somehow craft the aggregate initializer so that noncopyable initialization is skipped? (I tried eg things like Base{{},a}
without real understanding, but that did not work either: ~noncopyable
is protected).
Or will I need to explicitly define Base::Base
which will skip the noncopyable
initialization, using it from Derived::Derived
instead of the aggregate initialization?
The aggregate initialization of the base class you tried
Derived(int a): Base{{}, a} {}
// ^^
Would have worked if the constructor of boost::noncopyable
wasn't protected
(see here ).
The easiest fix should be to add a constructor to the base class.
#include <boost/core/noncopyable.hpp>
struct Base: private boost::noncopyable
{
int a;
Base(int a_) : a{a_} {}
};
struct Derived: public Base
{
Derived(int a): Base{a} {}
};
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.