Thread: Sending 'Messages' between objects

  1. #1
    Registered User
    Join Date
    Oct 2004
    Posts
    25

    Sending 'Messages' between objects

    Hey.

    I know one of the great advantages of OO is the ability for objects to communicate with each other. In the past, I have not managed to do this: instead I just have each method called from main (or wherever the core of my program is), return the results, then if necessary use that info to send to another object.

    My problem is: how would I send messages between objects? If each class is in a separate file, they shouldn't be directly accessible to each other. The only way I can think of is to have a pointer in every object to every other object, and send the messages to that.

    Am I missing something?

  2. #2
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    What exactly you want to do?

    Objects don't send messages to each other. They at best access members of other objects.
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Have one class use another class. That is how classes interact. One way to do this is to have one class use another class type for a member variable. For example, your class might use the string class for a member variable representing a name. When you set the value of that string, or call size(), or use the string member in any way, the classes are interacting (in one direction). To get this to work properly, your class header must #include the string header.

    You can do the same thing with two of your own classes. For example, you might have a class that represents an inventory. You might also have a class that represents a store. To allow the store to interact with inventory, you would #include the inventory class header in your store class header, and then add an inventory member variable to the store class.

    >> If each class is in a separate file, they shouldn't be directly accessible to each other.
    Sure they can. Hopefully the string class example shows how you can #include the header with one class definition inside a header that contains another class definition. There are many other ways a class can use another class without sending pointers around or passing objects from main. In most cases, one class uses the other, but not the other way around. Having two classes actually communicate back and forth with each other directly is as common and is a more advanced notion that I don't think you are talking about.

  4. #4
    semi-colon generator ChaosEngine's Avatar
    Join Date
    Sep 2005
    Location
    Chch, NZ
    Posts
    597
    in OO/UML speak calling a method on another class IS passing a message.

    Of course it's possible to have some generic message passing infrastructure (which can be useful), but in general it's slower and harder to code.
    "I saw a sign that said 'Drink Canada Dry', so I started"
    -- Brendan Behan

    Free Compiler: Visual C++ 2005 Express
    If you program in C++, you need Boost. You should also know how to use the Standard Library (STL). Want to make games? After reading this, I don't like WxWidgets anymore. Want to add some scripting to your App?

  5. #5
    Registered User
    Join Date
    Oct 2004
    Posts
    25
    OK, I think I get it.

    For some reason I thought there was a much larger gap between OO and procedural. What I am doing is basically procedural: with the main function calling all the other functions. It's just that now the functions are associated with a struct.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 11-23-2007, 02:10 AM
  2. Sending windows messages
    By Ideswa in forum Windows Programming
    Replies: 2
    Last Post: 03-02-2006, 01:27 PM
  3. problem with sending BN_CLICK messages
    By Bozyo in forum Windows Programming
    Replies: 3
    Last Post: 08-11-2004, 12:35 AM
  4. sending messages to a scrollbar
    By stormbringer in forum Windows Programming
    Replies: 1
    Last Post: 05-16-2003, 12:32 PM
  5. Edit controls not sending messages to parent window
    By EMiller in forum Windows Programming
    Replies: 5
    Last Post: 11-13-2001, 11:03 PM