Accessing a static Db object
Dear All,
I've created a database class in my project and I'm opening a db connection with that class whenever I need to query Db. However, it's very inefficient creata new database object in every function call and open a connection to database. For that reason I'm trying to change my implementation and create a static protected db object and open a connection once and close it when I finish all my queries.
So far, it sounds reasonable and easy :)
However, when I call "open" method of my database class from another class, I'm getting this error:
Quote:
error C2662: 'Database::Open' : cannot convert 'this' pointer from 'const Database' to 'Database &'
How can I solve this problem?
My db header:
Code:
#define CATCHERROR(ptr,a) catch(_com_error &e)\
{\
ErrorHandler(e,m_ErrStr);\
ptr=NULL;\
return a;\
}
#define CATCHERRGET catch(_com_error &e)\
{\
ErrorHandler(e,m_ErrStr);\
sprintf(m_ErrStr,"%s\n**For Field Name:%s",m_ErrStr,FieldName);\
return 0;\
}
#import "c:\Program Files\Common Files\System\ADO\msado15.dll" \
rename("EOF", "EndOfFile")
typedef ADODB::_RecordsetPtr RecPtr;
typedef ADODB::_ConnectionPtr CnnPtr;
class Database;
class Table;
class Database
{
public:
CnnPtr m_Cnn;
char m_ErrStr[500];
Database();
bool Open(char* UserName, char* Pwd,char* CnnStr);
bool Close();
bool OpenTbl(int Mode, char* CmdStr, Table& Tbl);
bool Execute(const char* CmdStr);
bool Execute(const char* CmdStr, Table& Tbl);
void GetErrorErrStr(char* ErrStr);
};
class Table{
public:
RecPtr m_Rec;
char m_ErrStr[500];
Table();
void GetErrorErrStr(char* ErrStr);
int ISEOF();
HRESULT MoveNext();
HRESULT MovePrevious();
HRESULT MoveFirst();
HRESULT MoveLast();
int AddNew();
int Update();
int Add(char* FieldName, char* FieldValue);
int Add(char* FieldName,int FieldValue);
int Add(char* FieldName,float FieldValue);
int Add(char* FieldName,double FieldValue);
int Add(char* FieldName,long FieldValue);
bool Get(char* FieldName, char* FieldValue);
bool Get(char* FieldName, std::string& FieldValue);
int GetInt(char* FieldName);
bool Get(char* FieldName,float& FieldValue);
bool Get(char* FieldName,double& FieldValue);
bool Get(char* FieldName,double& FieldValue,int Scale);
bool Get(char* FieldName,long& FieldValue);
};
Trying to call it from this class:
Code:
#include "Database.h"
namespace Myclass
{
class Myclass
{
protected:
static const Database* dbObj;
}
}
And implementation:
Code:
if(!dbObj->Open(userIdValue, passwordValue, CnnStr)){}
Thanks in advance
Re: Accessing a static Db object
You are trying to call a non-const function of the class via a pointer to a const object. The open function is not a const function. Do you need the database pointer to be declared as a pointer to a const object? You probably just wanted the pointer to be const, not the object itself.
Try this.
Code:
static Database* const dbObj;
Here is a short example that demonstrates the principle.
Code:
#include <iostream>
class ConstPointerOrConstData
{
public:
ConstPointerOrConstData(int i) : i(i) {}
void print() { std::cout << i << std::endl; }
private:
int i;
};
int main(int argc, char *argv[])
{
ConstPointerOrConstData value(5);
const ConstPointerOrConstData* valuePtrToConst = &value;
ConstPointerOrConstData* const constPtrToValue = &value;
constPtrToValue->print();
valuePtrToConst->print();
return 0;
}