基本クラスから多数 (50 以上) のクラスを作成する必要がありますが、唯一の違いは派生クラスの名前です。
たとえば、私の基本クラスは次のように定義されています。
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(){}
}
この基本クラスから多くの派生クラスを作成する必要があり、それぞれ独自のコンストラクタ、コピー コンストラクタ、および仮想デストラクタ関数を持ちます。現在、必要に応じて名前を変更してコードをコピー/貼り付けています。
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(){}
};
質問: テンプレートを使用してこれらのクラスを作成し、実装が繰り返されないようにする方法はありますか?