0

だから私は、そのままで本当に便利な基本機能をたくさん備えたストレージクラスを持っています。値による戻りを許可する移動コンストラクターがあります。

class A
{
public:
    virtual ~A(){}
    A(const A& a);
    A(A&& a);
    A& operator=(const A& rhs);
    A& operator=(A&& rhs);
    int foo();//example member function
    //example function returning by value, elision and RHR make it efficient
    static A Foo();
};

これは、A の所有者を明確に定義できるため、優れています。A を拡張するクラスを継承する必要があり、呼び出し関数の return ステートメントがポリモーフィズムを持つようになった場合、スマート ポインターを使用する唯一の "正しい" 方法はありますか? 別名

class B_interface : public A
{
public:
    virtual ~B_interface(){}
    virtual void FooBar() = 0;
};

class B : public B_interface
{
public:
    virtual ~B(){}
    B(const B& a);
    B(B&& a);
    B& operator=(const B& rhs);
    B& operator=(B&& rhs);
    virtual void FooBar() override;
    static shared_ptr<B> idontlikeit();
}

私はそれを回避する (おそらく悪い) 方法を考えました: 継承の代わりに構成を使用する場合: クラスには impl ptr に似たものが含まれています:

class B_interface
{
public:
    virtual void FooBar() = 0;
};


class B : public A
{
    shared_ptr<B_interface> impl_;//could use 
public:
    B(const A& a,shared_ptr<B_interface> interface)//or some smarter way to pass this in
    : A(a)//call a's copy constructor
    {
       impl_.reset(interface);
    }//this constructor thinks

    void FooBar() { impl_->FooBar();}
    virtual ~B(){}
    B(const B& a);
    B(B&& a);
    B& operator=(const B& rhs);
    B& operator=(B&& rhs);
    static B ilikeitbetter();
}

だから私はそのクラスでより良いクラスを使用するのが好きですが、クラスをそのクラスでちょっと悪臭を放ちます...また、B_interfaceはBの外ではあまり意味がないかもしれません...代替案はありますか?

4

1 に答える 1