I don't want to be harsh, but there are
tons of problems with this code. You have dropped pointers, shared ownership of memory, memory leaks, and a half dozen other major issues. I recommend scrapping this entirely and rewriting from scratch.
Your biggest problem is memory management related. Whenever you allocate memory, it needs to be cleaned up, and to keep track of what memory has been allocated, it's often best for each allocation to have a clear "owner". That owner is responsible for allocating and freeing the memory. When you start passing allocated blocks around and there's no clear ownership, you'll run into memory leaks and other issues that you're seeing.
You also seem to be misunderstanding the concept of inheritance. Why is Writer derived from CommonList? It doesn't seem to make sense in this context. Especially since Writer seems to have its own array of CommonLists. Inheritance forms an "is a" relationship. For example, deriving "Poodle" from "Dog" makes sense, because a Poodle "is a" Dog. This allows you to treat specific types of dogs generically (ie: if you have Poodle and BlackLab both deriving from Dog, you can write code that uses Dogs and it will work with all dogs the same -- not just specific types of dogs).
Here, it doesn't seem like Writer is a CommonList, so inheritance seems faulty.
Also, you can avoid lots of these headaches by avoiding dynamic allocation entirely. It's generally a good idea to try and avoid it unless you need it.
The idea is for the first writer to write down a word on the list, and then pass the list on to writer number two, |
The first writer should know nothing about the second writer - it shouldn't even be aware of its existence. Whoever owns the writers and the list should be the one in charge of passing the list around (in this case, main would probably be the owner)
From what your description sounds like... you want all writers to share a common list of words. This is actually very easy to do.
1) You need 1 list (owned by something higher up than each Writer -- probably owned by main)
2) You need X Writers (probably also owned by main)
3) You need the owner of those objects to give the Writers a pointer to the list.
--) Writers have no need to do any memory management
--) Use a container class for the list so it also doesn't have to do any memory management.
A simple way to do this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
|
#include <list> // for std::list
typedef list<string> WordList;
class Writer()
{
private:
WordList* theList;
public:
void SetWordList( WordList* v )
{
theList = v;
}
void IWantToAddAWordToTheList()
{
theList->push_back( "my word" );
}
};
int main()
{
WordList theOnlyWordList; // main owns the word list
vector<Writer> theWriters( number_of_writers ); // the array of writers
for(int i = 0; i < number_of_writers; ++i)
theWriters[i].SetWordList( &theOnlyWordList ); // give the word list to all writers
// ... you're done!
}
|
Notice:
- no need for new/delete
- minimal pointers getting passed around
- much simpler and easier to follow