Thread: nested classes

  1. #1
    Registered User
    Join Date
    Apr 2003

    Question nested classes

    First, here is my code:
    #include <iostream>
    #include <string>
    using namespace std;
    class message {
                string to;
                 string from;
                 string all;
                     class mailbox {
                                  void mailbox::add_message(message) {
                                     // do something to store the string in a file
    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;
    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!


  2. #2
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    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.


  3. #3
    Magically delicious LuckY's Avatar
    Join Date
    Oct 2001
    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:
    // a door containing a bunch of houses
    class Door {
      class House {
    when you want to be doing this:
    // 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
    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, 05:01 PM
  4. Nested Classes
    By manofsteel972 in forum C++ Programming
    Replies: 4
    Last Post: 11-21-2004, 11:57 AM
  5. nested classes...
    By doubleanti in forum A Brief History of
    Replies: 8
    Last Post: 12-09-2001, 04:25 PM