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
  • 51,717
  • 14
  • 111
  • 144
neo
  • 937
  • 1
  • 10
  • 22

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,477
  • 2
  • 40
  • 64
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
  • 245,575
  • 61
  • 446
  • 609