繼承我要做到以下幾點:C++從virtal基類
class ErrorBase
{
public:
void SetError(unsigned errorCode)
{
mErrorCode = errorCode;
}
char const* Explanation(unsigned errorCode) const
{
return errorExplanations[errorCode];
}
private:
char const* errorExplanations[];
unsigned mErrorCode;
};
class MyError : virtual public ErrorBase
{
public:
enum ErrorCodes {
eNone,
eGeneric,
eMySpecificError
};
MyError()
{
// I want this to refer to the parent class's attribute,
// such that when Explanation() is executed, it uses this
errorExplanations = {
"no error",
"error in MyClass",
"specific error"
}
}
~MyError() { }
};
但我上線在子類中聲明errorExplanations
以下錯誤:
error: expected primary-expression before '{' token
我如何在子類中聲明errorExplanations
,以便我可以實例化一個孩子,並調用myChild.Explanation()
並獲取在子構造函數中定義的錯誤字符串之一?
關於我的使用const
,virtual
,public
等的任何建議/更正,讚賞,謝謝!
與虛擬繼承無關 – 2010-06-24 16:35:16