Base
コンストラクターが引数として参照を取るbase class( ) があります。派生クラスのコンストラクターで、スーパークラス コンストラクターを呼び出します。もちろん、参照を引数として渡す必要があります。しかし、戻り値の型が値であるメソッドからその引数を取得する必要があります...
簡単な例を挙げます:
class Base
{
public:
Base(MyType &obj) { /* do something with the obj */}
};
class Derived : public Base
{
public:
Derived(MyOtherType *otherType) :
Base(otherType->getMyTypeObj()) // <--- Here is the error because (see *)
{
// *
// getMyTypeObj() returns a value and
// the Base constructor wants a reference...
}
};
class MyOtherType
{
public:
MyType getMyTypeObj()
{
MyType obj;
obj.setData( /* blah, blah, blah... Some data */);
return obj; // Return by value to avoid the returned reference goes out of scope.
}
};
どうすればこの問題を解決できますか?