2010-06-24 41 views
-1

繼承我要做到以下幾點: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等的任何建議/更正,讚賞,謝謝!

+0

與虛擬繼承無關 – 2010-06-24 16:35:16

回答

2

要麼你傳遞錯誤信息的陣列基類的構造函數(語法可能不是完美的,但希望你的想法):

class ErrorBase { 
    public: 
    ErrorBase(char const* errorExplanations[]) { 
     this->errorExplanations = errorExplanations; 
    } 
    ... 
    private: 
    char const* errorExplanations[]; 
    ... 
}; 

class MyError : virtual public ErrorBase { 
    public: 
    ... 
    MyError() : 
     ErrorBase({ 
     "no error", 
     "error in MyClass", 
     "specific error" 
     }) 
    { } 
    ... 
}; 

或者你讓Explanation虛擬,並提供所需的實現在派生類:

class ErrorBase { 
    public: 
    ... 
    virtual char const* Explanation(unsigned errorCode) const = 0; 
    protected: 
    unsigned mErrorCode; 
}; 

class MyError : virtual public ErrorBase { 
    public: 
    ... 
    MyError() : 
     errorExplanations({ 
     "no error", 
     "error in MyClass", 
     "specific error" 
     }) 
    { } 

    virtual char const* Explanation(unsigned errorCode) const { 
     return errorExplanations[errorCode]; 
    } 
    ... 
    private: 
    char const* errorExplanations[]; 
}; 
+0

我結束了使用第一個選項,謝謝你的回覆! – aaronstacy 2010-06-24 17:22:08

2

那麼,有一件事是錯誤的是,你不能指定到這樣的數組。你只能以這種方式初始化它們。由於您已經在構造函數的初始化部分初始化了數組(初始化爲空),因此您的數組將被初始化。

您需要以通常分配給數組的方式之一(如memcpy或for循環)將數組分配給數組。

另一個不對的地方是,您實際上沒有權限訪問您要分配給它的數組。您需要將其公開給受保護的子類或具有分配功能。

0

試着這麼做:

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 
    }; 

    char const* child_strings[] = {"no error", "error in MyClass", "specific error"}; 

    MyError() 
    { 

     // I want this to refer to the parent class's attribute, 
     // such that when Explanation() is executed, it uses this 
     errorExplanations = child_strings; 
    } 
    ~MyError() { } 
}; 

讓你的父類僅包含一個指針,讓您的孩子班級創建和初始化數組,然後使指針指向你的孩子的構造函數中的陣列。

2

這是另一種選擇。有基類通過虛函數獲取數據數組:

class ErrorBase 
{ 
    public: 
    void SetError(unsigned errorCode) 
    { 
     mErrorCode = errorCode; 
    } 

    char const* Explanation(unsigned errorCode) const 
    { 
     return GetErrorTable()[errorCode]; 
    } 

    private: 
    virtual char const **GetErrorTable() const = 0; 

    private: 
    unsigned mErrorCode; 

}; 

class MyError : virtual public ErrorBase 
{ 
    virtual char const **GetErrorTable() 
    { 
     static char const *data[] = { 
     "no error", 
     "error in MyClass", 
     "specific error" 
     }; 
     return data; 
    } 
}; 
0

另一種選擇:讓Explanation(unsigned) const功能virtual使派生類管理它們自己的機制用於查找錯誤信息:

class ErrorBase 
{ 
public: 
    virtual ~ErrorBase() { } 

    void SetError(unsigned errorCode) 
     : mErrorCode(errorCode) 
    { 
    } 

    char const* Explanation() const { return this->Explanation(mErrorCode); } 

    virtual char const* Explanation(unsigned errorCode) const 
    { 
     return errorExplanations[errorCode]; 
    } 

private: 
    unsigned mErrorCode; 
}; 

class MyError : virtual public ErrorBase 
{ 
public: 
    enum ErrorCode { 
     eNone = 0, 
     eGeneric = 1, 
     eMySpecificError = 2 
    }; 

    MyError(ErrorCode c) 
     : ErrorBase(static_cast<unsigned>(c)) 
    { 
    } 

    virtual ~MyError() { } 

    virtual char const* Explanation(unsigned errorCode) const; 
}; 

的然後將MyError錯誤代碼的錯誤字符串編譯爲目標文件:

// MyError.cpp 
static const char* s_errorExplanations[] = { 
     "no error", 
     "error in MyClass", 
     "specific error" 
    }; 

char const* MyError::Explanation(unsigned errorCode) const 
{ 
    return s_errorExplanations[errorCode]; 
}