0

I know this might be stupid, but I can't understand what's the usage of this scenario:

  1. I have a .cpp and .h with class definitions and methods(Let's say, class Class1)
  2. In Class2's .h file, it's written that class Class1 right before the class definition for Class2.

This is how the source code look:

Class1.h

class Class1
{ //Class 1 definition}

Class2.h

class Class1;
class Class2
{ //Class 2 definition}

So why does the class Class1 appear in Class2.h? It's a huge source code and I've sick of it as i unable to understand the overall flow. Any explaination will give a big help. Thanks for helping!

5 Answers5

5

That's a forward declaration.

You're essentially telling the compiler that Class1 is a class somewhere else. It doesn't necessarily have to know its actual structure.

This can be used to avoid cyclic references as well as cut dependencies.

In your given example, if you edit the contents of Class1, you don't have to recompile translation units using Class2 only, even though the class is known.

Mario
  • 35,726
  • 5
  • 62
  • 78
1
class Class1;
class Class2
{ //Class 2 definition}
};

The class Class1; is a forward reference. You can take the address or make a reference to objects of Class1. But you can't derence it .i.e. access any member as long as the actual Class1 definition is not visible.

harper
  • 13,345
  • 8
  • 56
  • 105
1

If Class2 need only pointers to Class1, then forward declaration is all what compiler need to define Class1*. Pointer to class is compiler type and need just information that type exists somewhere.

What you achieve here is:

  • no cyclic references (which is often a bed smell of architecture)
  • no dependencies
  • less to include
senfen
  • 877
  • 5
  • 21
  • @juanchopanza, what exactly is incorrect? What more than forward declaration of Class1 is needed for compiler to know how to generate Class1* type? My answer don't cover whole question but no one mentioned about it earlier and I found it connected. – senfen Mar 27 '15 at 09:38
  • OK, it isn't wrong, but it is misleadingly incomplete. There are many other situations where you can use forward declarations. – juanchopanza Mar 27 '15 at 09:39
  • Yes, I can agree with that. It is incomplete. I mention about it because this is most common case where I meet with forward declaration. – senfen Mar 27 '15 at 09:55
  • "only pointer" is wrong because it is legal to define a member in Class2 as 'Class1&'. That is a reference what is different from a pointer. – harper Apr 03 '15 at 19:42
  • so read once again and train logic :) I didn't write that forward declaration allow for using only pointers, but that: if you need use only pointers than forward declaration is enough. This don't imply anything about references. – senfen Apr 03 '15 at 19:46
0

It is called a forward declaration. You need this if Class2 uses Class1 but Class1 has not yet been defined or implemented.

arynaq
  • 6,710
  • 9
  • 44
  • 74
0

Probably the declaration of Class2 requires the prior declaration of Class1, because pointers or references to Class1 objects are used in Class2. Maybe they are stored as member data, or passed as function parameters.

Without the "class Class1;" line, the C++ compiler would have no idea what Class1 is.

This forward declaration makes compilation faster than including the whole header file for Class1. As long as only pointers or references to Class1 are used, the include is not necessary.

Rémi
  • 3,705
  • 1
  • 28
  • 39