In my C++ project, I have two classes, Particle
and Contact
. In the Particle
class, I have a member variable std::vector<Contact> contacts
which contains all contacts of a Particle
object, and corresponding member functions getContacts()
and addContact(Contact cont)
. Thus, in "Particle.h", I include "Contact.h".
In the Contact
class, I would like to add code to the constructor for Contact
that will call Particle::addContact(Contact cont)
, so that contacts
is updated for both the Particle
objects between which the Contact
object is being added. Thus, I would have to include "Particle.h" in "Contact.cpp".
My question is whether or not this is acceptable/good coding practice and, if not, what would be a better way to implement what I am trying to achieve (simply put, automatically updating the list of contacts for a specific particle whenever a new contact is created).
These classes will be tied together by a Network
class that will have N particles (std::vector<Particle> particles
) and Nc contacts (std::vector<Contact> contacts
). But I wanted to be able to have functions like particles[0].getContacts()
– is it okay to have such functions in the Particle
class in this case, or is there a better association "structure" in C++ for this purpose (of two related classes being used in another class).
I may need a perspective shift here in how I am approaching this. Since the two classes are connected by a Network
class object, is it typical code/class organization to have connectivity information entirely controlled by the Network
object (in that a Particle object should not be aware of its contacts and, consequently, it should not have a getContacts()
member function). Then, in order to know what contacts a specific particle has, I would need to obtain that information through the Network
object (e.g., using network.getContacts(Particle particle)
).
Would it be less typical (perhaps even discouraged) C++ class design for a Particle object to have that knowledge, as well (i.e., have multiple ways to access that information -- through either the Network object or the Particle object, whichever seems more convenient)?
Network
class object that containsParticle
objects andContact
objects. With that base knowledge, I can then try to assess whether or not it fits with my specific needs, which are still being explored/developed as I go along in the project. – AnInquiringMind Nov 8 '17 at 16:38