のconst参照を返す私はCに新たなんだ++と私はこれの戻り値の型に対処するかどうかはわかりません。C++オブジェクト
const Derived& myClass::getDerived(){} const
。
MYCLASSメンバ変数Base**b
有する:return b[indexOfDerived];
とエラーがある:私が試し
Class Derived : public Base
{
....
}
:reference to type 'const Derived' could not bind to an lvalue of type 'Base *'
Iも試み
#include "Base.h"
Class myClass
{
public:
virtual const Derived& getDerived() const;
.....
protected:
Base**b;
}
派生クラスは、ベースクラスから継承されます:return *this->b[indexOfDerived];
、エラー:no viable conversion from returned value of type 'Part' to function return type 'const CPU'
オブジェクトのconst参照を返すにはどうすればよいですか?私は困惑している。
私は、次のコードで、コンストラクタ内の変数Base**b
を初期化:不正な構文のため申し訳ありません
myClass::myClass()
{
b = new Base*[size];
for(int i = 0; i < size; i++)
{
b[i] = new Base();
}
}
....
// deallocating memory in destructor by using delete and delete[]
....
。
私は実際に感謝します!私はちょうどC + +を学ぶようになり、前に "dynamic_cast <>"または "static_cast <>"を見たことがありません。私はjavaとcでのキャストを理解していますが、まだC++でのキャストにはまだ紹介されていません。ありがとうございました! –