Thread: Problem accessing calling object members- please help

  1. #1
    Registered User
    Join Date
    Nov 2008
    Posts
    5

    Problem accessing calling object members- please help

    Hello,

    I have a problem that seemed easy to me at first, but now I cannot find a way to solve it. I have Object1 that establishes a database connection in its constructor. In one of Objects1 methods I create Object2.
    I want to access a database connection (established by Object1) in Objects2 constructor. Is this possible (I have to use Objects1 variable)?? How can I solve that?

  2. #2
    Registered User valaris's Avatar
    Join Date
    Jun 2008
    Location
    RING 0
    Posts
    507
    Make object2 take a db connection type in its constructor, assuming there is a handle of some sort to the database created in object1.

  3. #3
    Registered User
    Join Date
    Nov 2008
    Posts
    5
    Hmm, don't know why but I get a crash when I try to use pointer (handle) to database. Can database be used simultaneously in object1 in 2 with the same handle?

    I am also thinking of a different approach. I can declare object1 as parent of object2. Can I then somehow access parents variables? If yes, how?

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    If it crashes when you try to use the same pointer, you are probably doing something else wrong than violating some rule about the use of the database - perhaps you have freed the original pointer and keeping a copy, or overwritten the data the pointer points at?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Registered User
    Join Date
    Nov 2008
    Posts
    5
    Sorry, crash was caused by typo in a program. Now it is working! Thank you!

    So now I must pass handle to database to all functions in object2, that need it, right?

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Tiansen View Post
    Sorry, crash was caused by typo in a program. Now it is working! Thank you!

    So now I must pass handle to database to all functions in object2, that need it, right?
    Or you could make Object2 hold a handle in the class itself.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #7
    Registered User
    Join Date
    Nov 2008
    Posts
    5
    Yes, I must do that, because else I have troubles. How can I do that?

    I owe you a beer

  8. #8
    Registered User
    Join Date
    Nov 2008
    Posts
    5
    I think I figured it out. I make a variable (pointer2) in object2 and then make pointer2=pointer1 and it works.

    Thank you all for help!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Template object creation problem
    By babu198649 in forum C++ Programming
    Replies: 7
    Last Post: 09-16-2008, 04:02 AM
  2. Replies: 60
    Last Post: 12-20-2005, 11:36 PM
  3. Accessing structure members
    By foniks munkee in forum C Programming
    Replies: 18
    Last Post: 02-13-2002, 03:06 PM
  4. Replies: 3
    Last Post: 12-03-2001, 01:45 PM