MyTable 类

 
前两天用ODBC API 访问数据库,由于直接用ODBC API得到的数据没有什么数据结构,全是简单数据类型,用起来很麻烦,所以自己做了一个MyTable类用来组织数据。我只做了个框架,实现了几个最实用的功能,可能还有BUG,希望抛砖引玉,大家来完善它。
我在测试的时候发现std::string 类型的字符串用printf输出会出错,得用cout ,为什么呢?
还有就是visual studio 2005 的 C++ 编译器不支持类模板的分开实现,我只好把实现代码写在了头文件里,不知有没有优雅的方法?
希望高手不要笑,我很菜啊。
 
//MyTable.h
#ifndef MYTABLE_DEFINED
#define MYTABLE_DEFINED
 
#define MAX_ROW_COUNT       1024// 设置这个宏是为了控制表的大小
#define MAX_COLUMN_COUNT    1024// 别一不留神耗光了内存^_^
 
#include <string>
using namespace std;
 
template <class T>
class MyTable
{
public :
     // 构造函数和析构函数
     MyTable();
     MyTable(int RowCount,int ColumnCount);
     ~MyTable();
     // 功能函数
     bool          InitTable(int RowCount,int ColumnCount);
     bool          SetValue(int Row,int Column,const T &Value);
     bool          GetValue(int Row,int Column,T &Value) const;
     bool          DeleteValue(int Row,int Column);
     void          SetColumnName(int ColumnIndex,const string &Value);
     string        GetColumnName(int ColumnIndex);
     bool          IsInit();
     int           GetRowCount();
     int           GetColumnCount();
 
private :
     // 私有数据成员
     int           RowCount;
     int           ColumnCount;
     string        *ColumnName;
     T             **pData;
     bool          isInit;
};
 
 
 
 
// 类模板实现
 
 
// 构造函数和析构函数
template <class T>
MyTable<T>::MyTable()
{
     RowCount          = 0;
     ColumnCount        = 0;
     ColumnName         = NULL;
     pData              = NULL;
     isInit             = false;
}
 
 
template <class T>
MyTable<T>::MyTable(int RowCount,int ColumnCount)
{
     this->RowCount         = RowCount;
this ->ColumnCount      = ColumnCount;
     ColumnName             = new string[ColumnCount];
     pData                  = new T * [RowCount * ColumnCount];
     isInit                 = true;
 
     for(int i=0; i < ColumnCount; i++)
     {
         ColumnName[i] = "";
     }
    
     for(int i=0; i < RowCount * ColumnCount; i++)
     {
         pData[i] = NULL;
     }
}
 
 
template <class T>
MyTable<T>::~MyTable()
{
     for(int i=0; i < RowCount*ColumnCount; i++)
     {
         delete pData[i];
     }
     delete[] ColumnName;
     delete[] pData;
}
 
 
// 功能函数
template <class T>
bool MyTable<T>::InitTable(int RowCount,int ColumnCount)
{
     if(isInit == true)
         return false;
     this->RowCount         = RowCount;
     this->ColumnCount      = ColumnCount;
     ColumnName             = new string[ColumnCount];
     pData                  = new T * [RowCount * ColumnCount];
     isInit                 = true;
 
     for(int i=0; i < ColumnCount; i++)
     {
         ColumnName[i] = "";
     }
    
     for(int i=0; i < RowCount * ColumnCount; i++)
     {
         pData[i] = NULL;
     }
}
 
 
template <class T>
bool MyTable<T>::SetValue(int Row,int Column,const T &Value)
{
     int i = Row * ColumnCount + Column;
     if(i> RowCount * ColumnCount || pData == NULL)
         return false;     
 
     if(pData[i] != NULL)
     {
         delete pData[i];
     }
     pData[i] = new T(Value);
     if(pData[i] == NULL)
         return false;
     else
         return true;
}
 
 
template <class T>
bool MyTable<T>::GetValue(int Row,int Column,T &Value) const
{
     int i = Row * ColumnCount + Column;
     if(i> RowCount * ColumnCount || pData == NULL)
         return false;
 
     Value = *pData[i];
     return true;
}
 
 
template <class T>
bool MyTable<T>::DeleteValue(int Row,int Column)
{
     int i = Row * ColumnCount + Column;
     if(i> RowCount * ColumnCount || pData == NULL)
         return false;
     if(pData[i] != NULL)
     {
         delete pData[i];
     }
     return true;
}
 
template <class T>
void MyTable<T>::SetColumnName(int ColumnIndex,const string &Value)
{
     ColumnName[ColumnIndex] = Value;
}
    
 
 
template <class T>
string MyTable<T>::GetColumnName(int ColumnIndex)
{
     return ColumnName[ColumnIndex];
}
 
 
template <class T>
int  MyTable<T>::GetRowCount()
{
     return RowCount;
}
 
 
template <class T>
int  MyTable<T>::GetColumnCount()
{
     return ColumnCount;
}
 
 
template <class T>
bool MyTable<T>::IsInit()
{
     return isInit;
}
 
#endif
 
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值