如何使用C ++中的模板编程从基类创建派生类? [英] How to create derived classes from a base class using template programming in C++?

查看:97
本文介绍了如何使用C ++中的模板编程从基类创建派生类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要从基类创建多个类(超过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(){}

}

我必须从这个基类创建很多派生类,每个派生类都有自己的构造函数,复制构造函数和虚拟析构函数,目前我正在复制/粘贴代码,改变名称: p>

  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(){}
};

问题
是否有办法使用模板创建,因此不会重复执行?

解决方案

我想创建一个类层次结构,在你的catch子句中调度(依靠编译器找出正确的类型)来实现自定义错误处理。你可以保持 BaseError 类,然后添加一个模板类,然后提供多个实例化。请考虑这个:

  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(){}

具体错误(int errorCode,int osErrorCode,const std :: string& errorMessage)
:BaseError(errorCode,osErrorCode,errorMessage){}
};

现在可以设置几个类型定义:

  typedef ConcreteError< 0> FileError; 
typedef ConcreteError< 1>网络错误;
typedef ConcreteError< 2>数据库错误;
// ...

您现在具有一个具有三个不同错误类的层次结构。 / p>

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 ?

解决方案

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.

这篇关于如何使用C ++中的模板编程从基类创建派生类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆