コンストラクター Foo(Bar * b) を持つクラス Foo があります。その Bar を指す Foo を返すクラス Bar 内の関数が必要です。私は試します:
Foo& Bar::make_foo() {
Foo f((Bar*)this);
return f;
}
しかし、G ++は私に言います:
error: variable ‘Foo f’ has initializer but incomplete type
これで、ヒープメモリで問題なく動作することがわかりました。
Foo* Bar::make_foo() {
Foo * f = new Foo((Bar*)this);
return f;
}
編集:明らかに、問題は不完全なクラス定義が原因でした。ただし、スタック メモリ内のオブジェクトを返すための適切なイディオムがあるかどうかはまだ興味があります。