Jump to content

Inter-class communication?

- - - - -

  • Please log in to reply
4 replies to this topic

#1
sea

sea

    Newbie

  • Members
  • Pip
  • 7 posts
Hi, I've got classes set up in a header file as such:

class small_obj

{

        public:

        void do_something();

};

class bigger_obj

{

        private:

        list<string> msg_buffer;//Used for storing messages, I use list for sorting ability.

        small_obj contained_smaller_obj;


        public:

        bigger_obj()

               {

                        contained_smaller_obj.do_something();

               }

};

Now my question is, how would I go about appending to the message buffer msg_buffer from within the 'smaller_obj' class?
I'm not the best with C++ so I think it might have something to do with friend classes? Or maybe inheritance?
Any help would be appreciated. Thanks.

#2
ZekeDragon

ZekeDragon

    Writes binary right handed and hex left handed

  • Moderators
  • 2,103 posts

sea said:

how would I go about appending to the message buffer msg_buffer from within the 'smaller_obj' class?
You don't. Notice that the private label is above the list<string> object, this means that only instances of the class bigger_obj have access to that member. However, it is possible to give an address of the list<string> to the smaller object to be manipulated, but both the bigger_obj and small_obj must be modified to do specifically this. Allow me to demonstrate:
#include <iostream>
#include <string>
#include <vector>

using namespace std; // Not kosher but for simplicity's sake.

class SmallObj
{
public:
    void modifyList(vector<string> &list)
    {
        string x("This is another string to add.");
        list.push_back(x);
    }
};

class BigObj
{
public:
    void useModify()
    {
        small.modifyList(list);
    }

    void printStrings()
    {
        for (int i = 0; i < list.size(); ++i)
        {
            cout << list[i] << endl;
        }
    }

private:
    SmallObj small;
    vector<string> list;
};

int main(void)
{
    BigObj n;
    n.useModify();
    n.printStrings();
    cout << endl;
    n.useModify();
    n.printStrings();
}
Try compiling/executing this code, you should get three lines written. This shows that you are appending strings to the end of a vector of strings, despite the privacy (in this case however, I passed a reference rather than an address). If you pass an address to the small_obj, you can have it save that address and write to the bigger_obj's list<string> at any time.
Wow I changed my sig!

#3
sea

sea

    Newbie

  • Members
  • Pip
  • 7 posts
Ah ok. I figured I could use a pointer to get it done. It seems kinda messy to pass a pointer down through the classes though.
Just like there's a 'this' pointer, isn't there a 'parent' pointer or something similar? I'll use this method for now though.

#4
ZekeDragon

ZekeDragon

    Writes binary right handed and hex left handed

  • Moderators
  • 2,103 posts
No, there isn't. There are two reasons for this; first, there is no way to know whether or not an object will have a parent, there is no guarantee of such a thing. Second, it's not possible to know what the parent object's interface is, therefore you couldn't call any methods of that object or access any of it's properties. I know that Qt handles this situation by having a generic Object class that has an interface to apply to all objects (similar to what Java does), then it has an optional parameter on each of the Object class' subclasses to pass it a pointer to the parent. That's probably as elegant as you're gonna get it, which does feel messy, I know. Welcome to C++, though. :)
Wow I changed my sig!

#5
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others
What would make more sense is for do_something() to return a pointer to a buffer that larger could then assign to its buffer. Alternately, you could pass the buffer pointer as a parameter in do_something(buf) so that it can work.

You should always be thinking of communication in terms of functions handling the communication via functions parameters (references and pointers) along with the return values.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users