CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2

Thread: Accessing a static Db object

  1. #1
    Join Date
    Nov 2011

    Question 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:
    error C2662: 'Database::Open' : cannot convert 'this' pointer from 'const Database' to 'Database &'
    How can I solve this problem?

    My db header:
    #define CATCHERROR(ptr,a)	catch(_com_error &e)\
    								return a;\
    #define CATCHERRGET			catch(_com_error &e)\
    								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
    	CnnPtr m_Cnn;
    	char m_ErrStr[500];
    	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{
    	RecPtr m_Rec;
    	char m_ErrStr[500];
    	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:
    #include "Database.h"
    namespace Myclass
        class Myclass
    		static const Database* dbObj;
    And implementation:
    if(!dbObj->Open(userIdValue, passwordValue, CnnStr)){}
    Thanks in advance

  2. #2
    Join Date
    Aug 2005
    San Diego, CA

    Lightbulb 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.

    static Database* const  dbObj;
    Here is a short example that demonstrates the principle.
    #include <iostream>
    class ConstPointerOrConstData
        ConstPointerOrConstData(int i) : i(i) {}
        void print() { std::cout << i << std::endl; }
        int i;
    int main(int argc, char *argv[])
        ConstPointerOrConstData value(5);
        const ConstPointerOrConstData* valuePtrToConst = &value;
        ConstPointerOrConstData* const constPtrToValue = &value;
        return 0;
    Last edited by kempofighter; November 22nd, 2011 at 05:28 PM. Reason: adding example

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Windows Mobile Development Center

Click Here to Expand Forum to Full Width

On-Demand Webinars (sponsored)