Reputation: 331
I need to create a number of classes (more than 50) from a base class, where the only difference is in the names of the derived classes.
For example, my base class is defined as:
class BaseError : public std::exception
{
private:
int osErrorCode;
const std::string errorMsg;
public:
int ec;
BaseError () : std::exception(), errorMsg() {}
BaseError (int errorCode, int osErrCode, const std::string& msg)
: std::exception(), errorMsg(msg)
{
ec = errorCode;
osErrorCode = osErrCode;
}
BaseError (const BaseError& other)
: std::exception(other), errorMsg(other.errorMsg)
{
ec = other.errorCode;
osErrorCode = other.osErrorCode;
}
const std::string& errorMessage() const { return errorMsg; }
virtual ~BaseError() throw(){}
}
I have to create a lot of derived classes from this base class each having its own constructors, copy constructor and the virtual destructor function, currently I am copying/pasting the code changing the names where necessary :
class FileError : public BaseError{
private:
const std::string error_msg;
public:
FileError () :BaseError(), error_msg() {}
FileError (int errorCode, int osErrorCode, const std::string& errorMessage)
:BaseError(errorCode, osErrorCode, errorMessage){}
virtual ~FileError() throw(){}
};
Question: Is there some way to have these classes created using templates so the implementation is not repeated ?
Upvotes: 2
Views: 923
Reputation: 361802
If the implementation is exactly same, and you just want different name for each class, then simple typedef would do your job.
And if there is slight difference in implementation though not in interface, then you may probably need templates. Then consider also policy based design.
Upvotes: 0
Reputation: 94549
I suppose you want to create a class hierarchy so that you can use dynamic dispatch in your catch clauses (relying on the compiler to find out the correct type) to implement custom error handling. You could do this by keeping the BaseError
class as it is and then adding a template class for which you then provide multiple instantiations. Consider this:
class BaseError : public std::exception
{
private:
int osErrorCode;
const std::string errorMsg;
public:
int ec;
BaseError () : std::exception(), errorMsg() {}
BaseError (int errorCode, int osErrCode, const std::string& msg)
: std::exception(), errorMsg(msg)
{
ec = errorCode;
osErrorCode = osErrCode;
}
// ...
};
template <int T>
class ConcreteError : public BaseError {
public:
ConcreteError () :BaseError(), error_msg() {}
ConcreteError (int errorCode, int osErrorCode, const std::string& errorMessage)
:BaseError(errorCode, osErrorCode, errorMessage){}
};
You can now set up a few type definitions:
typedef ConcreteError<0> FileError;
typedef ConcreteError<1> NetworkError;
typedef ConcreteError<2> DatabaseError;
// ...
You now have a hierarchy with three distinct error classes.
Upvotes: 7
Reputation: 147036
If the implementations are identical, make an enum, and template on it.
enum error {
file_error,
};
template<error e> class my_exception : public BaseError {
....
};
typedef my_exception<file_error> file_exception;
Upvotes: 4