2

http://www.learncpp.com/cpp-tutorial/19-header-files/

It mentions the following as another solution to "forward declaration":

A header file only has to be written once, and it can be included in as many files as needed. This also helps with maintenance by minimizing the number of changes that need to be made if a function prototype ever changes (eg. by adding a new parameter).

But, cannot this also be made with "forward declaration"? Since we are defining the function int add(int x, int y) for example in "add.cpp", and using this function in "main.cpp" by typing:

int add(int x, int y);

? Thanks.

Antoine Pelisse
  • 12,871
  • 4
  • 34
  • 34
Simplicity
  • 47,404
  • 98
  • 256
  • 385

5 Answers5

4

That is certainly possible. But for a realistically-sized program, there will be a large number of functions that a large number of other files will need to declare. If you put a forward declaration in every file that needs to access another function, you have a multitude of problems:

  1. You've just copy-pasted the same declaration into many different files. If you ever change the function signature, you have to change every place you've pasted its forward declaration.
  2. The forward declaration itself does not naturally tell you what file the actual function is defined in. If you use a sane method of organizing your header files and your source files (for instance, every function defined in a .cpp file is declared in a .h file with the same name), then the place that the function is defined is implied by the place that it is declared.
  3. Your code will be less readable to other programmers, who are very used to using header files for everything (for good reason), even if all you need from a header is one specific function and you could easily forward-declare it yourself.
John Calsbeek
  • 35,947
  • 7
  • 94
  • 101
2

Header files contain forward declarations - that's what they do. The issue they resolve is when you have a more complex project with multiple source code files.

You could have a library of functions, e.g. matrix.c for matrix operations. Without header files you would have to copy the forward declarations for all the matrix.c functions into all the other source files. You would also have to keep all those copies up to date with any changes to matrix.c.

If you ever change the function in matrix.c, but forget to change its declaration in another file you will not get a compile error. You will probably not get a linker error either. All you will get is a crash or other random behaviour once you run your program.

Having the declarations in a single file, typically matrix.h, that will be used everywhere else removes all these issues.

thkala
  • 84,049
  • 23
  • 157
  • 201
2

You can use forward declaration but it doesn't scale well and it's unwieldly if you're using somebody else's code or library.

In general, the header file defines the interface to the code.

Also, think what happens if the function requires some user defined type. Are you going to forward declare that too? That type may regularly change its implementation (keeping it's public interface the same) which would result in having to regularly change all the forward declarations.

The header file solution is far more maintainable (less error prone) and make it far easier to determine exactly what code is being used.

plivesey
  • 59
  • 1
  • Can you just explain this more further "lso, think what happens if the function requires some user defined type. Are you going to forward declare that too? That type may regularly change its implementation (keeping it's public interface the same) which would result in having to regularly change all the forward declarations."? Thanks. – Simplicity Jan 21 '11 at 11:06
  • 1
    Imagine the function int foo(someClass arg); To call foo() you need to have declared foo() and someClass. someClass may be large, may only have a few public methods but might have a lot of private/protected methods and members. The public parts should not change but the other parts can. The header file method simplifies the whole process. #include "someClass.h" is so much easier than copy/pasting the class. If the class changes someClass.h will change and you get this automatically otherwise you become out of sync. someClass may require otherClass so someClass.h will include otherClass.h etc – plivesey Jan 21 '11 at 11:42
1

I C and C++ one essentially put all the forward and or external declarations into the header. This then provides a convenient way of including them in the various source files without having to manually include them.

In your case, if you have add defined in add.cpp, you can just provide the external declaration in main.cpp and everything is cool. The header file is there to help you when you have a large number of files that need add declared and don't want to do so for each one.

doron
  • 27,972
  • 12
  • 65
  • 103
  • When you say: "The header file is there to help you when you have a large number of files that need add declared and don't want to do so for each one.", when we want to used a "header file", shouldn't we declare it in ALL the files that need the "add" function? Thanks. – Simplicity Jan 21 '11 at 11:08
0
int add(int x, int y); // forward declaration using function prototype

Can you explain "forward declaration" more further? What is the problem if we use it in the main() function?

It's same as #include"add.h". If you know,preprocessor expands the file which you mention in #include, in the .cpp file where you write the #include directive. That means, if you write #include"add.h", you get the same thing, it is as if you doing "forward declaration".

I'm assuming that add.h has this line:

int add(int x, int y); 

What are forward declarations in C++?

Community
  • 1
  • 1
Nawaz
  • 353,942
  • 115
  • 666
  • 851