0

So I'm trying to create a base class for one of my projects that will have a couple classes inherit from it. My problem is I am trying to return a std::bitset from one of the methods, the problem is that in each of the sub classes will return different size bitsets, so I am looking for a way to declare a method that returns a bitset of an unknown size, well unknown to the base class known to the subclass. Additionally I have a guarantee that the base class is abstract and won't be used on its own.

csteifel
  • 2,854
  • 6
  • 35
  • 61

1 Answers1

6

You can't do that, the size of the std::bitset is part of its type, and different sized std::bitsets are different unrelated types. You can abstract the differences by using type-erasure, but in your particular case it would be simpler to just use a dynamic bitset, like the one available at Boost.

http://www.boost.org/doc/libs/release/libs/dynamic_bitset/dynamic_bitset.html

If your base class were just an utility class, not intended to be used from the outside world, then you could make it a template class parametrized on the std::bitset size:

template< int Size > class base
{
    typedef std::bitset< Size > bitset_type;
};

class derived : base< 128 > { ... };

But then each of your classes would inherit from a different unrelated base class.

K-ballo
  • 80,396
  • 20
  • 159
  • 169