私有继承和交换

我在两个非常相关的类的实现中使用私有继承。
using Base::X;
非常有用和优雅。但是,我似乎找不到重用基类交换功能的优雅解决方案。
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 )
   { /* ??? */ }
};
到目前为止,我唯一能想到的就是将
A::swap
的定义复制到
Const_A::swap
的定义中,YUCK! 是否有一个优雅的解决方案来重用私有基类的交换? 有没有更简洁的方法来实现我在这里尝试做的事情(一个类的const包装器)?     
已邀请:
好吧,你不能只打电话给基地的
swap
版本吗?
void swap( Const_A& other )
{
    A::swap(other); // swaps the `A` portion of `this`.
    // …
}
代替
,你通常只交换
Const_A
而不是
A
的成员,但由于你的特殊情况没有,这就是你应该需要的。     
您可以像其他方法一样:
void Const_A::swap( Const_A& other ) {
   A::swap(other);
   // here any specifics
}
    

要回复问题请先登录注册