Class instance within another class?

This is a discussion on Class instance within another class? within the C++ Programming forums, part of the General Programming Boards category; Hey I have a somewhat interesting situation / question. Let's say I had a class that was meant for database ...

  1. #1
    Registered User
    Join Date
    Oct 2006
    Posts
    118

    Class instance within another class?

    Hey

    I have a somewhat interesting situation / question. Let's say I had a class that was meant for database manipulation with the following code:

    Code:
    #include <iostream>
    
    using namespace std;
    
    SQL::database testDB("test.db");
    
    testDB << "SQL operation";
    And let's say I'm making a class of my own, in which I want to use an instance of SQL::database, just like I would in my main program.

    More specifically, I'm wondering if there's a way to create an instance of SQL::database as a private member of my own class, so that my classes functions can each reference to the instance.

    Thanks for the help, hope I was clear in my point!

    FlyingIsFun1217

  2. #2
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,262
    Of course:
    Code:
    class MyClass
    {
    public:
        MyClass(void) : testDB("test.db") {}
        void foo(void) { testDB << "SQL operation"; }
    
    private:
        SQL::database testDB;
    };
    bit∙hub [bit-huhb] n. A source and destination for information.

  3. #3
    Registered User
    Join Date
    Oct 2006
    Posts
    118
    Sweet! What is the term for this? I wouldn't mind reading up on it more.

    Thanks!

  4. #4
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,262
    Quote Originally Posted by FlyingIsFun1217 View Post
    Sweet! What is the term for this? I wouldn't mind reading up on it more.

    Thanks!
    Composition
    bit∙hub [bit-huhb] n. A source and destination for information.

  5. #5
    Registered User
    Join Date
    Oct 2006
    Posts
    118
    Thanks again! Something I love about the community here, always seems to be willing to help.

    FlyingIsFun1217

  6. #6
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,598
    You could also use aggregation since the SQL database lifetime probably would not be controlled by the objects using it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. help on class coupling
    By andrea72 in forum C++ Programming
    Replies: 4
    Last Post: 04-17-2011, 11:16 AM
  2. Stuck with function
    By scmurphy64 in forum C Programming
    Replies: 9
    Last Post: 11-10-2009, 11:41 AM
  3. deriving classes
    By l2u in forum C++ Programming
    Replies: 12
    Last Post: 01-15-2007, 05:01 PM
  4. My Window Class
    By Epo in forum Game Programming
    Replies: 2
    Last Post: 07-10-2005, 03:33 PM
  5. structure vs class
    By sana in forum C++ Programming
    Replies: 13
    Last Post: 12-02-2002, 07:18 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21