Let's imagine this would compile.
You Word
has 100 sentences
where each Sentence
has 200 words
(20000 Word
s here), each of which has 100 sentences
(2000000 Sentence
s here), each of which has 200 words
(400000000 Words
), each of which ...
I think I may stop here. You'll probably realize that this is not what you wanted to have.
You need to think again about your structure and you should take into account what a user or maintainer of such code would probably expect.
A sentence that contains several words -> Well ok that's logical.
A word that contains sentences? -> Why should a word ever contain sentences? Noone will ever expect this to be the case.
Furthermore: If you don't have a project that handles only sentences that always contain exactly 200 Words you may well consider using a dynamic memory structure like std::vector
.
class Sentence
{
private:
int wordNum;
std::vector<Word> words;
public:
...
};
If relationality is required you're of course free to use pointers.
If you want some kind of automated lifetime (if you have C++11 capability at hand) you could for example use a vector of std::shared_ptr
from sentences to words. -> Each sentences containing a specific word has its own shared ptr to that word object. If all sentences with a specific word go out of scope / are destroyed, the word will be removed, too.
I'd still recommend to stick to something that enables you to make use of RAII.