0

I was going through a c++ code and saw a header file with below declaration:

file a.h
class xyz;

But that class was not defined any where in the file. I also wrote a code and it perfectly compiles fine. Just wanted to know what is the use of declaring a class like this as it does't enforces programmer to define.

Thanks in advance!!

Mike Dinescu
  • 54,171
  • 16
  • 118
  • 151
neo
  • 969
  • 1
  • 11
  • 23

2 Answers2

0

this is a forward declaration of a class. you can use xyz* variables in the header file - the linker will resolve the actual class implementation later

NirMH
  • 4,769
  • 3
  • 44
  • 69
0

That's a forward declaration (or simply declaration) and it allows you to use the name xyz in contexts where a full definition isn't required.

One example is when you have another class which contains a pointer to a xyz:

class xyz;
class Aux
{
  xyz* k;
};

Other uses are return types, parameter types and references.

Luchian Grigore
  • 253,575
  • 64
  • 457
  • 625