多くの循環依存トピックを読みましたが、そのすべてが宣言に関係しているようです。私は相互に依存するオブジェクトを構築する方法と、私のアプローチで潜在的な落とし穴があるかどうかについて興味を持っています。この単純な例を考えてみましょう:C++循環依存性:コンストラクタはどのように見えるでしょうか?
#include <iostream>
#include <vector>
using namespace std;
class A; //Forward declaration
class B{
public:
B(string name, A* a):myA(a), name(name){
cout << "Works with pointer" << endl;
};
private:
A* myA;
string name;
};
class A{
public:
A(){
cout << "Constructing A" << endl;
if(bs.empty()) cout << "Vector is empty" << endl;
bs.push_back(B("First", this));
cout << "Array has " << bs.size() << " elements." << endl;
};
private:
std::vector<B> bs;
};
int main() {
cout << "Start" << endl;
A a;
cout << "Ok." << endl;
return 0;
}
は、私がB
でA*
ポインタを避けるために何ができるものはありますか?
理想的には私は参照を持っていると思いますが、私はB(string name, A& a)
にB
のコンストラクタを変更してbs.bush_back(B("First", *this));
に一backを変更した場合、私はエラーを取得:non-static reference member 'A& B::myA', can't use default assignment operator
を。 See the modified example
私がコンパイラによって合成されたはここには当てはまりません。どのように適切なoperator=
のようになりますか?あるいは、間違った方向を一緒にしていますか?あなたが標準コンテナタイプを使用する場合
あなたは円形持ちの場合それを削除して、より多くの回避策を探すべきではありません。 –
ええと、私の(言語にとらわれない)ドメインモデルには循環依存があります。クラス設計レベルで問題を回避する方法はありますか? – sebastiangeiger