nested classes

This is a discussion on nested classes within the C++ Programming forums, part of the General Programming Boards category; First, here is my code: Code: #include <iostream> #include <string> using namespace std; class message { public: message(); ~message(); string ...

  1. #1
    Registered User
    Join Date
    Apr 2003
    Posts
    18

    Question nested classes

    First, here is my code:
    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    class message {
    
         public:
                message();
                ~message();
                string to;
                 string from;
                 string all;
    
                     class mailbox {
    
                           public:
                                  void mailbox::add_message(message) {
                                     // do something to store the string in a file
                                  }
                           private:
    
                     };
         private:
    
         };
    message::message() {
         to = "to";
         from = "from";
         all = to + from;
         // create a string with the message in it
    }
    
    message::~message() {
    }
    int main() {
    message bob;
    message::mailbox homer;
    homer.add_message(bob);
    
    }
    Now what I want to do with this is have the message class create a message, and then have the mailbox class store that message. But I'm having problems trying to visualize how to move the information in the new message object to the mailbox. This may be a stupid question, but classes make my head spin, and I can't for the life of me make it do what I want. Thanks in advance!

    jim

  2. #2
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,668
    You need to stop the spinning!

    Read this page on object design, maybe it will slow the spin.

    You should also design how everything will work before you start writting code, not vica-versa.

    gg

  3. #3
    Magically delicious LuckY's Avatar
    Join Date
    Oct 2001
    Posts
    856
    You're dealing with a has-a relationship, but doing it in reverse. A mailbox has messages in it. A message does not have mailboxes in it. What you are doing is sort of like doing this:
    Code:
    // a door containing a bunch of houses
    class Door {
      class House {
        //...
      };
    };
    when you want to be doing this:
    Code:
    // a house containing a bunch of door
    class House {
      class Door {
        //...
      };
    };
    Follow that link codeplug gave you. It should help clear the fog of war.

    (100 posts!)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Anyone with a spare java class file(s) with nested classes?
    By Sebastiani in forum A Brief History of Cprogramming.com
    Replies: 6
    Last Post: 04-29-2009, 08:33 PM
  2. Problem with nested classes
    By Bargi in forum C++ Programming
    Replies: 5
    Last Post: 05-29-2008, 05:52 AM
  3. deriving classes
    By l2u in forum C++ Programming
    Replies: 12
    Last Post: 01-15-2007, 04:01 PM
  4. Nested Classes
    By manofsteel972 in forum C++ Programming
    Replies: 4
    Last Post: 11-21-2004, 10:57 AM
  5. nested classes...
    By doubleanti in forum A Brief History of Cprogramming.com
    Replies: 8
    Last Post: 12-09-2001, 03:25 PM

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