派生自模板化的基类

Deriving from templatized base class

本文关键字:基类 派生      更新时间:2023-10-16

我必须建立一个包含不同类型对象的小型数据库。拥有一个基本的模板化类是个好主意,让我们称之为数据库,然后让从中派生其他类。

例如:

template< typename Record,
          size_t RECORD_SIZE,
          char RECORD_SEP = '!',
          char RECORD_PARAM_SEP = ',',
          char RECORD_FIELD_SEP = '~',
          size_t MAX_RECORDS_PER_QUERY = 5000,
          size_t MIN_RECORD_COUNT = 15000
          >
class Database
{
public:
    typedef Record Record_t;
    typedef std::vector< unsigned char > QueryBuffer;
    Database( const std::string& basePath, const std::string& recordPath, const std::string tableFilename );
    enum class QueryResult
    {
        OK,
        NO_DATA,
        OVERFLOW,
        FUTURE_DATE,
        FUTURE_RANGE,
        ERROR
    };
    void add( void add( const Record_t& r )
    {
        Lock lock( mMutex );
        // ... lots of stuff here
    }
    QueryResult query(QueryBuffer &qb, time_t first, time_t last )
    {
        Lock lock( mMutex );
        // ... lots of stuff here
    }
    QueryResult query(QueryBuffer &qb, time_t first )
    {
        Lock lock( mMutex );
        // ... lots of stuff here
    }
    // protected and private data
};

然后以这种方式派生类

class GameDatabase : public Database< GameType, 9 >
{
private:
    using MyBase Database< GameType, 9 >;
public:
    GameDatabase( const std::string& basePath )
        : MyBase{ basePath, "g/", "g.dat" }
    {
    }
    // another query method
    QueryResult query(QueryBuffer &qb, size_t first, size_t last )
    {
        Lock lock( mMutex );
        // use protected methods of MyBase
        // .... lots of stuff here      
    }
};

这是一个好主意还是更好的解决方案?问题是数据库的定义必须全部内联,这是长代码。

这不是一个不寻常的成语,但通常模板参数要么是类型,要么是需要编译时的整数。您希望通过这样编译大小和分隔符来获得什么?

我建议你让它们成为数据库的成员,在构造函数中初始化。如果您想内联所有内容,请继续。但是,当您只需要它们成为常量时,不要将它们制作为模板参数。

您不需要内联实现模板成员,它们只需要在标头中。一个常见的习惯用法是为实现提供一个单独的标头,并将其包含在主标头的底部。

// foo.hpp
template <typename T>
class foo {
public:
  void bar();
};
#include "foo.tpp"
// foo.tpp
template <typename T>
void foo<T>::bar() { /* code here */ }