Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Simple Newbie question about accessing a Variable out of a class of a class

Reply
Thread Tools

Simple Newbie question about accessing a Variable out of a class of a class

 
 
Christian Maier
Guest
Posts: n/a
 
      02-14-2007
Hi

After compiling "Hello World", I am on the way to code a shared
library - but now I have some trouble. I have a class which is holding
a Database connection. In this class there are other classes holding
data from sql statements. For now I cannot access the connection
object in the classes of the "connectonholding"-class, which is what I
need.

Here I paste my code and marked the sentence where the problem is,
probably you can better imagine what I want.

Thanks for your Help!
Christian Maier


//foo.h
#include <pqxx/pqxx>

using namespace PGSTD;
using namespace pqxx;

typedef long long llong;
class FiMi
{
public:
void setDB(string);
void setHost(string);
void setPort(string);
void setUser(string);
void setPasswd(string);
void setSchema(string);
void doConnect();
void doDisconnect();
connection * getConnection;
private:
string db;
string host;
string port;
string user;
string passwd;
string schema;
string connectionstring;
connection dbCon(string);

public:
class fProvider
// I must have access to the connection object
{
public:
long long getProvider(string); //from
caption
void getAllProviders(string[]);
void getAllProviderTypes(string[]);
long long getProvideType(string); //from
caption
void addProvider(string,long long, string); //caption,
ptype_id, additional
void delProvider(long long); //via
provider_id
void updProvider(long long); //via
provider_id
};
};


//foo.cpp
#include <pqxx/pqxx>
#include "foo.h"
using namespace PGSTD;
using namespace pqxx;

typedef long long llong;
void FiMi::doConnect()
{

connectionstring = "dbname=" + db + " " +
"host=" + host + " " +
"port=" + port + " " +
"user=" + user + " " +
"password=" + passwd;

try
{
cout << "ich bin hier " + connectionstring + " \n";
connection dbCon(connectionstring);
cout << "connected to " << dbCon.dbname() << " \n";

//probably I want to use the connection from client code for
native sqls ....
getConnection = &dbCon;
}
catch (const broken_connection &e)
{
cout << "beim verbinen ist folgender fehler passiert:\n";
throw logic_error( e.what() );
}

};


llong FiMi::fProvider::getProvider(string caption)
{
try
{
/* ####################### HERE IS WHAT I NEED
###########################
THE dbCon Object is no access habe her which is what i
need, how to do this
cout << dbCon.Database << endl;

################################################## ####################
*/
return 0;
}
catch (const sql_error &e)
{
cerr << "SQL error: " << e.what() << endl
<< "Query was: '" << e.query() << "'" << endl;
return -1;
}
};

int main( int argc, char *argv[] )
{
FiMi a;
return 0;
}

 
Reply With Quote
 
 
 
 
John Harrison
Guest
Posts: n/a
 
      02-14-2007
Christian Maier wrote:
> Hi
>
> After compiling "Hello World", I am on the way to code a shared
> library - but now I have some trouble.


That's a big leap.

I have a class which is holding
> a Database connection. In this class there are other classes holding
> data from sql statements. For now I cannot access the connection
> object in the classes of the "connectonholding"-class, which is what I
> need.


Well it no different from any case where you have information in one
place which you need to access in another place. You must pass the
connection object as a parameter to the other class.
>
>
> llong FiMi::fProvider::getProvider(string caption)
> {
> try
> {
> /* ####################### HERE IS WHAT I NEED
> ###########################
> THE dbCon Object is no access habe her which is what i
> need, how to do this
> cout << dbCon.Database << endl;
>
> ################################################## ####################
> */


Two changes, move the fProvider class out of the FiMi. As you've already
found having one class inside another does not give the inside access
any special access to members of the outer class C++ is not Java).
Secondly add a FiMi parameter to your getProvider method, like this.

llong fProvider::getProvider(string caption, FiMi& dbCon)

This is going to be a long journey, and you're only just starting.

john
 
Reply With Quote
 
 
 
 
Christian Maier
Guest
Posts: n/a
 
      02-15-2007
Thanks for the hint, I have done a Step back and switched fom c++ to
c. This seems to be easier for me.

Chris

 
Reply With Quote
 
John Harrison
Guest
Posts: n/a
 
      02-15-2007
Christian Maier wrote:
> Thanks for the hint, I have done a Step back and switched fom c++ to
> c. This seems to be easier for me.
>
> Chris
>


No problem. I learned C before C++. I found it very helpful in switching
to C++ to think 'how would I write the equivlent code in C'. In fact
early C++ compilers did exactly that, they translated C++ into C. Now I
just think in C++ though and I'm much happier but for some reason some
people still perfer C.

john
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Newbie Q. Accessing a variable in one method from another. Mike Barnard Java 7 06-23-2010 08:43 PM
Newbie question: accessing global variable on multiprocessor amit C Programming 24 01-18-2010 02:25 PM
Accessing class variable at class creation time Carlos Python 7 09-26-2005 07:14 PM
Newbie Question: Global variable vs. Top-level variable Sam Kong Ruby 2 05-25-2005 09:47 PM
Accessing a global variable when there is a local variable in the same name Mohanasundaram C Programming 44 08-23-2004 11:17 PM



Advertisments