I want to do the following:
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() { }
};
But I get the following error on the line declaring errorExplanations in the child class:
error: expected primary-expression before ‘{‘ token
How do I declare errorExplanations in the child class such that I can instantiate a child, and call myChild.Explanation() and get one of the error strings defined in the child’s constructor?
Any suggestions/corrections regarding my usage of const, virtual, public, etc are appreciated, Thanks!
Either you pass the array of error messages to the base class in its constructor (syntax may not be perfect but hopefully you get the idea):
Or you make
Explanationvirtual and provide the desired implementation in the derived class: