Henning Koehler
Henning Koehler

Reputation: 2637

copying all super-class constructors

I would like to add a new constructor to an existing class (extending boost::dynamic_bitset to allow const char* as argument). As this doesn't seem possible to do directly, I'd be ok with creating a subclass boost::dynamic_bitset_plus instead which allows me to add the new constructor. However, the class has a whole bunch of other constructors, which I would like to preserve. Is there a way to do this without explicitly implementing each and every one of them?

Basically I could just do this:

namespace boost
{
    template <typename Block, typename Allocator>
    class dynamic_bitset_plus<Block, Allocator> : public boost::dynamic_bitset<Block, Allocator>
    {
    public:
        // new constructor
        explicit dynamic_bitset_plus(const char* s) : boost::dynamic_bitset<Block, Allocator>(std::string(s)) {}
        // existing constructors - boring but needed
        template <typename CharT, typename Traits, typename Alloc>
        explicit dynamic_bitset_plus(const std::basic_string<CharT, Traits, Alloc>& s,
            typename std::basic_string<CharT, Traits, Alloc>::size_type pos = 0,
            typename std::basic_string<CharT, Traits, Alloc>::size_type n = std::basic_string<CharT, Traits, Alloc>::npos,
            const Allocator& alloc = Allocator()) : boost::dynamic_bitset<Block, Allocator>(s, pos, n, alloc) {}
        // and a bunch of others ...
    };
}

but I'm looking for a way to avoid specifying all the existing constructors.

I do realize that you could simply write a function that takes a const char* and returns a boost::dynamic_bitset, or simply call std::string(...) whenever creating a new boost::dynamic_bitset, but this does get tedious and looks a bit ugly IMO.

Upvotes: 0

Views: 65

Answers (1)

Mark Ransom
Mark Ransom

Reputation: 308138

The using directive can pull in the base class constructors. When I tried it I found that it didn't include the default constructor, but that one is easy to include; an empty default contructor will of course call the base-class one implicitly.

template <typename Block, typename Allocator>
class dynamic_bitset_plus<Block, Allocator> : public boost::dynamic_bitset<Block, Allocator>
{
public:
    // default constructor
    dynamic_bitset_plus() {}
    // new constructor
    explicit dynamic_bitset_plus(const char* s) : boost::dynamic_bitset<Block, Allocator>(std::string(s)) {}
    // existing constructors
    using boost::dynamic_bitset<Block, Allocator>::dynamic_bitset;
};

Upvotes: 2

Related Questions