私有继承和交换
我在两个非常相关的类的实现中使用私有继承。
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包装器)?
没有找到相关结果
已邀请:
2 个回复
旦彤
版本吗?
代替
,你通常只交换
而不是
的成员,但由于你的特殊情况没有,这就是你应该需要的。
功飘