when doing this i had to include DataClass.h in ItemClass.h. when i did this it caused more errors since the 2 classes were essentially doing an infinite loop. |
That's weird,
#pragma once
should handle exactly this.
---
A reference is somewhat like a pointer. Passing an argument by reference allows you to access the original object inside the function (it is not copied). You can think of it as an implicitly deferenced pointer. The biggest differences are that a reference is not nullable, and it
must be initialized when it is defined (that means in the constructor initialization list if it's a member variable).
If you don't know how to use references I suggest you look for a tutorial as they are a powerful tool and I'm not sure I can explain them properly.
The important thing here is, if you
declare a function that uses a reference you don't need to #include any header to make a legal
declaration. Forward declaring the type of the reference is enough. Then, in the implementation file (e.g. ItemClass.cpp), you #include the header to access the details of the referenced object. Since the inclusion doesn't happen in any header file you are sure you won't have any recursive inclusion problems.
Forward declaring a type can be done with references and pointers, and indeed
should be done wherever possible.