I’m updating some code and while I was working in a header, I came across the following line.
.
.
.
class HereIsMyClass;
.
.
.
That’s it. It’s just one line that precedes another, longer class definition. HereIsMyClass is in fact another class somewhere else, but I don’t understand why this line is written here. What does it do?
This line in C++ is a forward declaration. It’s stating that at some point in the future a class named HereIsMyClass will likely exist. It allows for you to use a class in a declaration before it’s completely defined.
It’s helpful for both breaking up circularly dependent classes and header file management.
For example