C++ Classes

C++ Classes

I have written a program with two classes. Each uses the other within it and contains pointers to the other. However, this means that the first class encountered by the compiler has no previous class to use, so I get errors. How can I overcome this?

This problem is known as circular referencing. There are different solutions depending on how your code is structured.

First let’s assume that both class declarations are in the same headerfile (.h). For example, lets say we have classes Master and Dog. Master references Dog and Dog references Master, but it will not be possible for one or the other depending the order of the class declarations. So, how could you declare the Master class first; for instance, one that contains a reference to a Dog class that has yet to be recognized by the compiler. In this case you would add a forward declaration for the Dog class prior to declaring the Master class. This will indicate to the compiler that the Dog class declaration will follow eventually, but prior to it being explicitly utilized in the implementation of the Master class (see code =extract below).

// MasterAndDog.h// Forward class declaration of Dogclass Dog;// Master class declarationclass Master{// Operationspublic:	void Contact(Dog* pDog);	void SendCommand(const char* cmd);// Attributesprivate:	Dog* m_pDog;};// Dog class declarationclass Dog{// Operationspublic:	void Contact(Master* pMaster);	void AskForAttention( );// Attributesprivate:	Master* m_pMaster;};
A more typical C++ application will have a separate header file and methods file for each class defined. In that case you could do something similar to above with a few enhancements. Since the class declarations are in different header files, you would have to include forward declarations of the other class in both header files, and include the header files of the forward declared class to each method’s file, similar to the following:
// Master.h// Forward class declaration of Dogclass Dog;// Master class declarationclass Master{…}// Master.cpp#include “Master.h”#include “Dog.h”/* Master class definition follows… */****************************************************************// dog.h// Forward class declaration of Masterclass Master;// Dog class declarationclass Dog{…}// Dog.cpp#include “Dog.h”#include “Master.h”/* Dog class definition follows… */
Another alternative for this scenario is to simply remove the forward declarations from the header file and include the header file of the other class to this header file. Therefore there is no need to include the header files of the other class to methods files. For example:
// Master.h#include “dog.h”  // Dog class// Master class declarationclass Master{…}// Master.cpp#include “Master.h”/* Master class definition follows… */****************************************************************// dog.h#include “master.h”// Dog class declarationclass Dog{…}// Dog.cpp#include “Dog.h”/* Dog class definition follows… */
However, I don’t recommend this approach, because even if you have an incremental compiler, both Master and Dog source files will recompile if you change the class declaration in one or the other. All the compiler does is determine that the interface of a class has changed; therefore, any other class header that includes the header of the class interface that changed should be recompiled also to ensure a continued functioning application. Hence you have an unnecessary ripple effect.

The best solution is to avoid circular referencing altogether by decoupling the classes from one another, and incorporate a Mediator class through which all other classes interact by letting the Mediator class determine the effects that should happen to other classes involved. The end result is that you can easily extend or revamp parts of your system infrastructure without being severely limited by interdependencies.

Share the Post:
Heading photo, Metadata.

What is Metadata?

What is metadata? Well, It’s an odd concept to wrap your head around. Metadata is essentially the secondary layer of data that tracks details about the “regular” data. The regular

XDR solutions

The Benefits of Using XDR Solutions

Cybercriminals constantly adapt their strategies, developing newer, more powerful, and intelligent ways to attack your network. Since security professionals must innovate as well, more conventional endpoint detection solutions have evolved

AI is revolutionizing fraud detection

How AI is Revolutionizing Fraud Detection

Artificial intelligence – commonly known as AI – means a form of technology with multiple uses. As a result, it has become extremely valuable to a number of businesses across

AI innovation

Companies Leading AI Innovation in 2023

Artificial intelligence (AI) has been transforming industries and revolutionizing business operations. AI’s potential to enhance efficiency and productivity has become crucial to many businesses. As we move into 2023, several

data fivetran pricing

Fivetran Pricing Explained

One of the biggest trends of the 21st century is the massive surge in analytics. Analytics is the process of utilizing data to drive future decision-making. With so much of

kubernetes logging

Kubernetes Logging: What You Need to Know

Kubernetes from Google is one of the most popular open-source and free container management solutions made to make managing and deploying applications easier. It has a solid architecture that makes