开发者

Private inheritance and swap

I'm using private inheritance in the implementation of two very related classes. The using Base::X; is very useful and elegant. However, I can't seem to find an elegant solution for reusing the base class's swap function.

class A
{
public:
   iterator       begin();
   const_iterator begin() const;
   const_iterator cbegin() const;

   A clone();

   void swap( A& other );
};

class Const_A : private A
{
public:
   // I think using A::A; will be valid in C++0x
   Const_A( const A& copy) : A(copy) { }

   // very elegant, concise, meaningful
   using A::cbegin;

   // I'd love to write using A::begin;, but I only want the const overload
   // this is just forwarding to the const overload, still elegant
   const_iterator begin() const
   { return A::begin(); }

   // A little more work than just forwarding the function but still uber simple
   Const_A clone()
   { return Const_A(A::clone()); }

   // What should I do here?
   void swap( Const_A& other )
   { /* ??? */ }
};

So far the 开发者_StackOverflowonly thing I can come up with is copy-pasting A::swap's definition into Const_A::swap's definition, YUCK!

Is there an elegant solution to to reuse the private base class's swap?

Is there a cleaner way to implement what I'm trying to do here (a const wrapper for a class)?


Well, can’t you just call the base’s version of swap?

void swap( Const_A& other )
{
    A::swap(other); // swaps the `A` portion of `this`.
    // …
}

In place of , you’d normally swap the members pertaining only to Const_A, not A but since there aren’t any in your particular case, this is all that you should need.


You can do as with the rest of the methods:

void Const_A::swap( Const_A& other ) {
   A::swap(other);
   // here any specifics
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜