VC++6编程 与ADO有关的定义类LRunSql的头文件代码

来源:百度知道 编辑:UC知道 时间:2024/06/03 17:58:33
以下内容是与ADO有关的定义类LRunSql的头文件代码,其中
class CDrawDlg;
void DateToInt(CString date,int& year,int& month,int& day);
bool OpenPrintFrame(CWnd* parent,CDrawDlg * m_draw);
在LRunSql的cpp文件里有上面两个函数的定义。我不明白为什么把它们放在这个头文件里,而不把它们放在class CDrawDlg的头文件和cpp文件里?

#ifndef LRUNSQL
#define LRUNSQL

class CDrawDlg;
void DateToInt(CString date,int& year,int& month,int& day);
bool OpenPrintFrame(CWnd* parent,CDrawDlg * m_draw);

#define XS 0
#define XSTH 1
#define RK 2
#define RKTH 3

class LRunSql
{
public:
LRunSql();
~LRunSql();
bool RunSQL(CString sql);
bool CheckSQLResult(CString sql);
public:
static _ConnectionPtr m_database;

static bool RollbackTrans();
static bool CommitTrans();
static bool BeginTrans();
static bool Close();
static bool InitConnectPtr();
static bool ConnectDataBase();

_RecordsetPtr m_recordset;
}

class CDrawDlg;
void DateToInt(CString date,int& year,int& month,int& day);
bool OpenPrintFrame(CWnd* parent,CDrawDlg * m_draw);
这个为什么会放在类LRunSql的头文件里?因为这些不是类的定义代码,而是声明,和函数的声明。因为在类LRunSql的cpp文件中,会需要用到上面这两个函数。而这两个函数中有 CDrawDlg类型的参数,我们知道,对于一种变量类型,必须先定义才能使用。因此这里先声明class CDrawDlg,说明有这么一个类,但他的定义在其他的地方(当然,在这里就是它对应的头文件和cpp了),这样下面的可以使用他了。不然的话,你在定义上面的两个函数是,他会报错,说没有CDrawDlg这种变量类型。
如果不这样,你也可以将CDrawDlg的.h文件包含进来,这样也可以使用这种类型了,但有时会遇到重定义之类的问题,尤其是你在多个地方都这么包含的话。

其实这种情况经常会发生,你去看看MFC里的代码就会发现,他就用了很多这样的,有时你自己也需要这么做。