views:

288

answers:

4

Suppose I have the following declaration:

class Over1
{
   protected:
      class Under1
      {
      };
};

I know that I could do the following:

class Over2 : public Over1
{
   protected:
        class Under2 : public Under1
        {
        };
};

But is there a way to declare Under2 without Over2?

Since you would have to extend Over1 to use any derivative of Under1 this may seem silly, but in this situation there might be 30 different flavors of Under. I can either:

  • Put them all inside Over1 : Not attractive since Over2 may only use 1 or 2 of them
  • Put them each in their own version of Over : Not attractive since then you will have to multiply inherit from almost the same class.
  • Find a way to create children of Under1 without creating children of Over1

So is this possible?

Thanks.

+1  A: 

Better than creating nested classes, you might want to look at embedding those closses into a namespace. That way, you don't need the outer class in order to get the inner class. There are some great arguments for and against in the Google C++ Style Guide.

Douglas Mayle
+1  A: 

Using templates and explicit specializations you can do this with just one additional class declaration in Over1.

class Over1
{
protected:
  class Under1
  {
  };

  template <typename T>
  class UnderImplementor;
};

struct Under2Tag;
struct Under3Tag;
struct Under4Tag;

template <>
class Over1::UnderImplementor<Under2Tag> : public Over1::Under1
{
};

template <>
class Over1::UnderImplementor<Under3Tag> : public Over1::Under1
{
};

template <>
class Over1::UnderImplementor<Under4Tag> : public Over1::Under1
{
};

Hope this helps.

Richard Corden
A: 

If you want to keep Under1 protected, then by definition, you need to inherit from Over1 to access it. I would suggest making Under1 public, or using namespaces as Douglas suggested.

I don't have the compiler to test these out right now, so I'm not at all sure that these would work, but you could try this:

class Over1
{
   protected:
      class Under1
      {
      };

   public:
      class Under1Interface : public Under1 
      {
      };
};

class Under2 : public Over1::Under1Interface
{
};

Or perhaps something like this:

class Over1
{
   protected:
      class Under1
      {
      };
};

class Under2 : private Over1, public Over1::Under1
{
};

Or even:

class Under2;

class Over1
{
   friend class Under2;

   protected:
      class Under1
      {
      };
};

class Under2 : public Over1::Under1
{
};

Although that would expose all of Over1s privates to Under2 - not likely something you'd want.

Eclipse
A: 

Sounds like a bit of a funky design to me. Why not try structuring your code differently. I think this could possibly be a good candidate for the decorator pattern, where you could wrap your base class in various decorators to achieve the desired functionality, the various flavors of 'under' could be decorators. Just a thought, hard to tell without knowing more about the intentions of your code.

jhufford