2

Possible Duplicate:
When to use forward declaration?

What is the use of specifying other class names above a particular class?

class A;
class B;

class C
{
 ...
 ...
};
Community
  • 1
  • 1
cppcoder
  • 22,227
  • 6
  • 56
  • 81
  • Check the Q I marked as duplicate, it explains what can be done and what cant be done when using forward declarations. – Alok Save Jul 02 '11 at 18:43

4 Answers4

8

It's a forward declaration.

As a hangover from C, the C++ compiler works in a top-to-bottom fashion (more or less). If you want your class C to refer to class A or class B in some way (via a pointer or a reference), then the compiler must know about their existence beforehand.

Oliver Charlesworth
  • 267,707
  • 33
  • 569
  • 680
  • What about in this case? `class A; class B { func(A a); }` So in this case, I need not include header which defines class A and just forward declare A? – cppcoder Jul 02 '11 at 18:30
  • No, you can only declare a pointer or reference to an `A` in that case. – Ed S. Jul 02 '11 at 18:35
  • 1
    It is not just a hangover from C. If it were not so the compiler would have to remember each undeclared usage and come back to it at the end of compilation if the name was not declared until then. That would slow down compilation time. The other way would be to parse the code multiple times which would also mean an immense slowdown. – Nobody moving away from SE Jul 02 '11 at 18:53
  • 1
    @Nobody: Indeed. But other languages (Java, etc.) manage perfectly fine with this. The situation is, of course, more complicated in C++ with templates. – Oliver Charlesworth Jul 02 '11 at 19:12
  • I am not an expert in java but as far as I know it is interpreted bytecode so the interpreter only needs the information at executiontime, where every needed library is loaded or an error thrown if the name is not known. – Nobody moving away from SE Jul 02 '11 at 20:12
  • @Nobody: The compiler must know what methods, etc. are available at compile-time. – Oliver Charlesworth Jul 02 '11 at 21:20
  • Of course. I said nothing about compilers ;) I was just trying to explain why Java or other interpreters avoid this problem. – Nobody moving away from SE Jul 02 '11 at 22:49
3

That is called a forward declaration. It allows you to declare a pointer to the type without including its definition. Of course, since the definition doesn't exist at this pointas far as the compiler is concerned, you can only declare pointers or references to the type as the compiler would not know how to construct an object of said type, i.e., it cannot not determine its size.

Ed S.
  • 122,712
  • 22
  • 185
  • 265
1

By forward declaring classes like that, you are telling the compiler that the classes exist, and not to worry, you will link them in later.

This is instead of including the header files with the full definition, and is sufficient if the compiler doesn't need to know what the class looks like. For example if you are only using pointers or references to the class in the current file.

Pianosaurus
  • 5,538
  • 2
  • 20
  • 15
0

This is called forward declaration and allows the use of those classes in the defined class's prototype.

Puppy
  • 144,682
  • 38
  • 256
  • 465