Reputation: 19075
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 e.g. 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?
Upvotes: 1
Views: 112
Reputation: 12779
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} {}
};
Upvotes: 1