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

Discussion in 'C++' started by Christian Maier, Feb 14, 2007.

  1. 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;
    }
    Christian Maier, Feb 14, 2007
    #1
    1. Advertising

  2. Re: Simple Newbie question about accessing a Variable out of a classof a class

    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
    John Harrison, Feb 14, 2007
    #2
    1. Advertising

  3. Thanks for the hint, I have done a Step back and switched fom c++ to
    c. This seems to be easier for me.

    Chris
    Christian Maier, Feb 15, 2007
    #3
  4. Re: Simple Newbie question about accessing a Variable out of a classof a class

    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
    John Harrison, Feb 15, 2007
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Carlos
    Replies:
    7
    Views:
    279
    Dave Hansen
    Sep 26, 2005
  2. Mohanasundaram
    Replies:
    44
    Views:
    1,039
    Keith Thompson
    Aug 24, 2004
  3. amit
    Replies:
    24
    Views:
    1,032
    Nobody
    Jan 18, 2010
  4. Mike Barnard
    Replies:
    7
    Views:
    281
  5. Sam Kong
    Replies:
    2
    Views:
    162
    Joel VanderWerf
    May 25, 2005
Loading...

Share This Page