I have a base class which looks something like this:
class Base
{
public:
typedef std::shared_ptr<Base> ptr_t;
typedef std::weak_ptr<Base> wptr_t;
enum class Type { foo, bar, baz };
Type x;
// ...
};
I’d like those internal types to be public so that I can do stuff like Base::ptr_t my_ptr(new Base); and so on. But if I make a new class like this…
class Derived : public Base
{
// ...
};
unfortunately, Derived::ptr_t is still a Base pointer. I’d like Derived to publicly inherit x from Base, but not inherit ptr_t,wptr_t, or Type. For example
Derived a;
a.x = Base::Type::foo; // this should work
a.x = Derived::Type::foo; // but I want this to fail
Is this possible, perhaps though some magic use of friend or virtual or something like that?
Based on the answers of iammilind and Luc Danton, here’s what I’ve come up with:
As far as I can tell, the only problem with this solution is that it adds a new and potentially confusing class. The class is defined in such a way that it can’t really be misused –
Baseitself cannot be derived from except byBaseClass, but still,BaseClassis an unattractive piece of namespace-clutter.However, for the particular piece of code that I intend to use this in, I happen to be using the equivalent of
BaseClassalready to solve an unrelated problem. So thisBaseClasstechnique suits my purposes just fine.