Global function

hi, im absolute beginner to c++. i got some doubts in MEMORY MANAGEMENT, and GLOBAL SHARING of functions.

1) Memory Management : i want to know, how to delete namespace ?[like objects]

2) I have a connection module , which needs to be accessed by several cpp files. what is the best way to do it?
now im using namespace....

Thnks
BeamRamana

1) Namespaces are a coding- and compiling-time conception, you cannot manipulate them during run-time.

2) What is the type of "mudule"? If it's a class, you usually #include <classname.h> into all those "several cpp files".
connection.h,connection.cpp are the HEADER file/CPP, in which i defined the connection routine.

im having more than 20 cpps. i need this connection.h in all modules to access the database.

I thought , it will take more memory , if we create object in 20 cpps.
Instead , is there a way , that i can declare it as a globala function. and access frm 20 cpps?

which is the efficient way? [creating 20 objs] or
defining a global funciton??

Thnks
BeamRamana

I thought , it will take more memory , if we create object in 20 cpps.


If your header file contains only declarations and no implementation (what must be taken as a rule) the #include will not take any memory. Declarations serve to give the compiler information about how the classes and routines are organized. The object code is at one file connection.obj after compilation.

You misunderstand the meaning of global functions. Global function is not a member of any class. Global or not global function can be used in other souce files. But each .cpp that references it must contain its declaration anyway.

Many #inlclude's may affect compilation time, not the size of executable or the speed of running of the program.
thnks for the reply

i have definitions too. following are my modules.
i want to use more efficiency in memory handling. This is a common module accessible by 20 different cpps.

pls tell me the best way to implement?


DBConnection.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
class DBConnection
{
    public:
            DBConnection();
            QSqlDatabase cn;
            QSqlQuery q;
            void createConnection();
            void openConnection();
            void closeConnection();
            bool initializeConnections();
            bool IsLastError();
            QSqlQuery SelectQuery(QString Query);
            bool DataBaseCommand(QString Query);
            bool DataBaseCommand(QSqlQuery SqlQuery);
            int Size(QSqlQuery);
            void clearAll();
            bool CreateDefaultTables();
            bool PasswordCheck(QString);
      private:
            void DisplayError(QSqlError Err);
};




DBconnection.cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
#include "DBConnection.h"
#include <QtGui>
#include <sqlite3.h>

bool LastError;

DBConnection::DBConnection()
{
}

void DBConnection::createConnection()
{
    cn= QSqlDatabase::addDatabase(DB_DRIVER);
    initializeConnections();
}

void DBConnection::openConnection()
{
    cn=QSqlDatabase::database();
}

void DBConnection::closeConnection()
{
    //cn.close();
    //cn.~QSqlDatabase();

    /*if(QSqlDatabase::contains(ConName))
    {
        qDebug()<<QSqlDatabase::contains(ConName);
        if(QSqlDatabase::contains(ConName))
        {
            qDebug()<<ConName;
        }
        //QSqlDatabase::removeDatabase(ConName);
    }*/
}

QSqlQuery DBConnection::SelectQuery(QString Query)
{
    LastError=false;
    //QSqlQuery rs(QString::null,cn);
    if(cn.isOpen())
    {
        if(!(q.exec(Query)))
        {
            QMessageBox::critical(0,"Error",   q.lastError().text());
            LastError=true;
        }
    }
    else
    {
        QMessageBox::critical(0,"Error", "Connection is Closed" );
        LastError=true;
    }
    return q;
}

bool DBConnection::initializeConnections()
{
      try
     {
        LastError=false;
        cn.setDatabaseName(DB_DBNAME);
        if ( ! cn.open() ) {
            LastError=true;
            QMessageBox::critical(0,"Error", "Cannot open database:"+ cn.lastError().text());
            return FALSE;
        }
        return TRUE;
     }
    catch(...)
    {
         LastError=true;
         QMessageBox::critical(0,"Error", "Query Execution Error" );
         return FALSE;
    }
}
bool DBConnection::DataBaseCommand(QString StrQuery)
{
        LastError=false;
        //QSqlQuery qrs(cn);

        if(cn.isOpen())
         {
            q.clear();
            if(!(q.exec(StrQuery)))
            {
               LastError=true;
               if(q.lastError().number()!=19)
               {
                   QMessageBox::critical(0,"Error",   q.lastError().text());
                   q.clear();
                   return false;
               }
               // false;
                else
                {
                    QVariant v = q.driver()->handle();
                    if (v.isValid() && qstrcmp(v.typeName(), "sqlite3*")==0)
                    {
                         sqlite3 *handle = *static_cast<sqlite3 **>(v.data());
                         if (handle != 0)
                         {
                            qDebug("Error: %s", sqlite3_errmsg(handle));
                            QMessageBox::about(0,"Trigger",sqlite3_errmsg(handle));
                         }
                     }
                    q.clear();
                    return false;
                }
            }
            if(q.isActive()==false)
            {
                QVariant v = q.driver()->handle();
                if (v.isValid() && qstrcmp(v.typeName(), "sqlite3*")==0)
                {
                     sqlite3 *handle = *static_cast<sqlite3 **>(v.data());
                     if (handle != 0)
                     {
                        qDebug("Error: %s", sqlite3_errmsg(handle));
                        QMessageBox::about(0,"Trigger",sqlite3_errmsg(handle));
                     }
                 }
                QMessageBox::about(0,"err",q.lastError().text());
            }
        }
        else
        {
            LastError=true;
            QMessageBox::critical(0,"Error", "Connection is Closed" );
        }
        q.clear();
        return true;
}

bool DBConnection::DataBaseCommand(QSqlQuery SqlQuery)
{
    LastError=false;
    if(cn.isOpen())
     {
        if(!(SqlQuery.exec()))
        {
           LastError=true;
           QMessageBox::critical(0,"Error",   SqlQuery.lastError().text());
           SqlQuery.clear();
      }
    }
    else
    {
        SqlQuery.clear();
        LastError=true;
        QMessageBox::critical(0,"Error", "Connection is Closed" );
    }
    SqlQuery.clear();
    return true;
}

bool DBConnection::IsLastError()
{
    if(LastError) return true;
    else return false;
}

int DBConnection::Size(QSqlQuery query)
{
    int size=0;
    while(query.next())
    {
        size++;
    }
        query.clear();
        return size;
}

void DBConnection::clearAll()
{
    q.clear();
}
That looks ok. I can't see what the problem is?

You may want to make LastError a private member of DBConnection.

It may be better to make DBConnection just deal with database connection and not queries. Take a look at SQLAPI+ for an example.
Topic archived. No new replies allowed.