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.