class Girl
{
public:
Guy boyfriend;
}
class Guy
{
public:
Girl girlfriend;
}
Declarining Girl girlfriend within class Guy is alright, but I also want to declare Guy boyfriend in class Girl, but I can't do that because one class must always be declared before the other. What's the way around this?
class Girl ;
class Guy ;
class Girl
{
// ...
public:
Guy& boyfriend_r ;
Guy* boyfriend_p ;
std::reference_wrapper<Guy> boyfriend_w ;
};
class Guy
{
// ...
public:
Girl& girlfriend_r ;
Girl* girlfriend_p ;
std::reference_wrapper<Girl> girlfriend_w ;
};
If a Girl will always have the same boyfriend, use a reference.
If a Girl may sometimes not have a boyfriend, use a (if appropriate, smart) pointer.
If a Girl will always have a boyfriend, but it may be a different boyfriend at different points in time, use a reference_wrapper.
This looks a good start for me (apparently I still need to understand better pointers). Is it easy to modify the code if the girl can have multiple boyfriends at the same time (let's say up to 5 simultaneous boyfriends)? I tried putting an array there, but I guess I still don't understand the language/syntax enough to make that work.