5

Possible Duplicate:
What is forward declaration in c++?

I just have a question about what a piece of code is doing in this simple example. I've looked up friend classes and understand how they work, but I don't understand what the class declaration at the top is actually doing (i.e. Robot). Does this just mean that the Happy class can use Robot objects but they can't access its private parts, any information would be appreciated.

#include <stdlib.h>
#include <stdexcept>

template <typename T>   // What is this called when included 
class Robot;            // is there a special name for defining a class in this way

template <typename T>
class Happy
{ 
  friend class Joe<T>;  
  friend class Robot<Happy<T> >;
  // ...
};
Community
  • 1
  • 1
Ockham
  • 435
  • 1
  • 6
  • 15

4 Answers4

7

It's a forward declaration.

It's just there to inform the compiler that a class template named Robot will be defined later and that it should expect that definition.

In the meantime (i.e., until Robot is officially defined), it allows the programmer to refer to that type in the code. In the example you've given, it is necessary to declare Robot as a friend of the Happy class. (Who picked these names?!)

It's a common strategy to avoid circular dependencies and minimize compilation time/overhead.

Cody Gray
  • 222,280
  • 47
  • 466
  • 543
  • +1, the common used term is *forward declaration*, even though the formal term is just *declaration*. The *forward* is usually added to make it explicit that we are dealing with just a declaration and not a *definition* (that is also a *declaration*) – David Rodríguez - dribeas Apr 13 '12 at 22:01
  • I'm working on a project that makes a forward declaration of class `A` in class `B` header file. I noticed I can comment the forward declaration and instead import the header file for class `A` and seems to work the same. Is there a reason to use one or the other? – Gonzalo Aug 09 '15 at 19:03
0

It basically says to the rest of the code "You can use this, but I will declare it later". Without that line, this piece of code would not compile: friend class Robot<Happy<T> >;

Simon Forsberg
  • 12,447
  • 9
  • 56
  • 96
0

It's called a forward declaration - it basically says to the compiler; "Trust me, I'm going to use a class that I will define later". But because the compiler doesn't know anything about the members of robot you can only use it here in ways where there is only a pointer (or reference) to it.

It's because otherwise you would need to include the Robot header in this header which might lead to other circular dependancies.

Martin Beckett
  • 90,457
  • 25
  • 178
  • 252
0

That's called a "forward declaration". You are telling the compiler "there's a class called Robot. I'm not explaining what it looks like, but be aware that it exists". That way, the compiler won't throw an "undefined identifier" error when it sees you use Robot inside the definition for Happy. You still have to include a complete definition for Robot somewhere, though, or you'll end up with linker errors.

You typically see this used when an object needs to reference something that's defined farther down in the .h file, or when two objects need to reference each other.

bta
  • 39,855
  • 5
  • 67
  • 92