Thread: Messaging with composition

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Chad Johnson
    Join Date
    May 2004
    Posts
    154

    Question Messaging with composition

    *EDIT: a better title probably may have been, "Messaging between objects"*

    Suppose I have the following (please forgive syntax errors):
    Code:
    class Module {
      public:
        Component *getComponent(type) {
          switch (type) {
            case "whatever":
              return new WhateverComponent();
              break;
          }
        }
    
        virtual String getModuleName();
    }
    
    class MyModule : public Module {
      public:
        String getModuleName() { return "MyModule"; }
    }
    
    class Component {
      void getContent() {
        ...
        // I want to call Module->getModuleName() here
        ...
      }
    }
    
    class SubComponent : public Component {
      ...
    }
    
    
    Module module = new MyModule();
    Component component = module->getComponent();
    String content = component->getBody();
    How do I get the module name (via getModuleName()) inside getContent() in Component to get the name associated with the Module subclass that instantiated the component?

    Would a good approach be to pass a pointer or reference to the Module (using "this") in the Component constructor in getComponent() when instantiating the Component?
    Code:
    class Module {
        ...
        Component *getComponent(type) {
          switch (type) {
            case "whatever":
              return new WhateverComponent(this);
              break;
          }
        }
        ...
    }
    
    class Component {
      private:
        Module *module;
    
      void getContent() {
        ...
        cout << this->module->getModuleName();
        ...
      }
    }
    Any better approach(es)?
    Last edited by ChadJohnson; 01-22-2008 at 11:20 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 11-10-2007, 03:51 PM
  2. Simple Instant Messaging
    By mrafcho001 in forum Tech Board
    Replies: 1
    Last Post: 03-30-2006, 05:04 PM
  3. Winsock Messaging Program
    By Morgul in forum Windows Programming
    Replies: 13
    Last Post: 04-25-2005, 04:00 PM
  4. Simulation of electronic messaging
    By c_help in forum C Programming
    Replies: 9
    Last Post: 02-19-2005, 11:18 AM
  5. Inheritance vs Composition
    By Panopticon in forum C++ Programming
    Replies: 11
    Last Post: 01-20-2003, 04:41 AM