首页 | 编程语言 | 网站建设 | 游戏天堂 | 冲浪宝典 | 网络安全 | 操作系统 | 软件时空 | 硬件指南 | 病毒相关 | IT 认证
软讯网络 > 编程语言 > C/C++ > MyTable 类
【标  题】:MyTable 类
【关键字】:MyTable
【来  源】:http://blog.csdn.net/maxttyl/archive/2006/12/30/1468203.aspx

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
 
 
约瑟夫环VC2005:【上一篇】
xoWidgets Foundation 软件和文档的版本发布规则:【下一篇】
【相关文章】
没有相关文章
【随机文章】
  • 红旗4。1实用命令
  • IIS攻击大全 二
  • 解惑宽带接入
  • 服务器租用与托管的最佳选择--中国福网
  • 查看安全模式启动日志文件
  • compress函数与uncompress函数
  • SuSE Linux 10.1最简单的挂载Windows分区
  • 拒绝打印机被非法共享
  • 全向天线Anttena
  • EVA 硬碟空間的使用與探討
  • 【相关评论】
    没有相关评论
    【发表评论】
    姓名:
    邮件:
    随机码*
    评论*
          
    |  首 页  |  版权声明  |  联系我们   |  网站地图  |
    CopyRight © 2004-2007 软讯网络 All Rigths Reserved.