Workaround for circular references between C++ classes - c++

Right now I have two classes which both have pointers to each other. When I try to compile it has an error because the program cannot compile one class without already having compiled the other.
It works like this
Main.cpp includes class A
Header guard for class A
Class A includes class B
Class B includes class A
Class A already got included so the header guard prevents loading it again
Class B has no idea what class A is
I just need to be able to reference one class from the other. I'm not incredibly skilled with C++ so this may be obvious but help would be appreciated.

Related

Weird C++ design pattern: Class inheritance via include files

I came across a C++ coding pattern that is so weird that I'd really like to know where it comes from. Some classes (base classes, no ancestors) need to share certain functionality. Normally you would implement this as an ancestor class (a template if need be) from which those classes inherit which need the functionality.
Here, the programmer chose to pack the required method and data member declarations into a separate file and include this file in midst of the class declaration. They even #define'd a symbol to the name of the class being defined before the #include line. This created declarations like this:
class CMyStructure
{
public:
CMyStructure();
~CMyStructure();
__int64 m_SomeData;
double m_SomeOtherData;
#define DEFINE_CLASS CMyStructure
#include "include_definitions.h"
#undef DEFINE_CLASS
};
The included code made use of the DEFINE_CLASS symbol.
The same pattern is repeated in the implementation file, where a different file is included which defines some methods.
Does this pattern have a name? Where does it come from?
(A warning to C++ newbies: Don't use this pattern!)
Thanks, Hans

Access Derived Class When Only Base Class Header is Included

I'm trying to understand if there is a way set an include directive for a base class but also be able to access derived classes that are defined in a different header than the base class. For instance:
In GenericObject.h:
class GenericObject {
/* whatever - shared properties and methods implemented in .cpp file */
}
In Ball.h:
class Ball : public GenericObject {
/* whatever - implementation details are in the .cpp file */
}
Now in some other implementation file (let's call it the main.cpp), I have #included "GenericObject.h". In main.cpp, I want to be able to access both the members of the GenericObject class as well as the Ball class and any other derived class of GenericObject. From my trials so far, the compiler won't recognize the Ball identifier if I try to declare it like Ball * b; and thus any of the members that belong to its class.
Is this because my #include'd "GenericObject.h" file is not also linking that there is this derived Ball class for me to access? I can correctly ID and use the Ball class if I "#include "Ball.h" but I can't imagine I should include all of the header files of derived classes of the GenericObject class, right?
If I had to take a guess at how this might work, I'd guess that something like a prototype declaration for the Ball class inside the GenericObject class would be a step in the right direction but I'm not sure if a class prototype is even a thing.
Thanks for reading and any feedback everyone!
No, you can't use any identifier that's not yet described. This includes any class, whether it derives from a base or not.
It seems to me you are violating Liskov. If you receive a reference or pointer to a base then you should not be doing anything that requires you know that there is a derived or not. You should only be using the interface of the base. Thereby you no longer need to include the headers for derived classes where you're not using something defined only in THEIR interface.
I can't imagine I should include all of the header files of derived classes of the GenericObject class, right?
If you want to access any class derived from GenericObject, other than through a pointer or a reference, you will need to #include the header file for them.
If you can get by with using just pointers and references, you don't need to #include the header files but you still have to forward declare them.

Duplicate class declaration in the header files

I am working with a c++ library that is not written by me.
Some header files include other classes, but still have sort of "duplicate" empty definition of the class. What is the purpose of this behaviour?
#include "OtherClass.h"
class OtherClass;
class ThisClass {
// definitions here
};
This is a forward declaration of the class which is a declaration without a complete definition of the identifier. This helps reducing the compile time and also the circular dependencies.
In your scenario you only should include or forward declare the class, but not both.

C++ project layout

I am completely confused as to the proper way to layout a C++ project.
I had all my classes in separate .cpp files, with their definitions in .h files. I then had one "header.h" which contained all the class headers, external dependencies and a few other things. But I wasn't able to use class names in the header files, where I needed to declare a pointer to one.
Can someone please explain the proper object-orientated layout for a C++ project.
You can fix the problem "wasn't able to use class names in the header files, where I needed to declare a pointer to one" by using forward class declarations, like:
class myClass;
However, having every class include a header.h that then includes every class is overkill. Instead, you should have each class specifically include only the classes and external dependencies that it actually needs.

class relationships UML diagrams

I have my class structure as follows
add.h has class add that has method int add(int,int) and add.cpp includes add.h and defines method add above
sub.h has class sub that has method int sub(int,int)
and sub.cpp includes sub.h and defines method sub
now, main.h has class main and it includes add.h and sub.h; main class also has some print methods to print results;main.cpp uses method add and sub to do calculations
My question is, what is the relationship between class main & class add also class main & class sub. Main.h simply includes add.h and sub.h, so is there any name for this relations.
AFAIK It is not inheritance, it is not compositon or aggregation.
You are mixing up the concepts of C++ compilation and class relationships - the two things really have nothing to do with each other. In the event that you want to model the relationships between the C++ source files, you should use a UML Component Diagram, but few people bother with this.
You have 3 independent classes. main will only use add and sub in its implementation. I don't see any relation between them.
As Neil says: the source files (and header files) happen to coincide with the classes here. That means that you don't model the relation between a.cpp and a.h: it's the declaration and the definition of class a, and the fact that they're separated is no design issue, merely a compilation artifact.
In general, when a .cpp file includes a .h file not of it's own class, you can say that the .cpp uses what's in the .h. When class a's declaration needs class b's declaration, probably a is aggregating class b.
In this case, I would say that the (plain, one-directional) relation between main and the 'operations' is to be labeled as 'usage'.
Next to that, it's common to give the sub and add classes a common superclass/interface.