Simplicity
Simplicity

Reputation: 48916

Header files vs. forward declaration

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.

Upvotes: 2

Views: 5301

Answers (5)

plivesey
plivesey

Reputation: 59

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.

Upvotes: 2

thkala
thkala

Reputation: 86333

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.

Upvotes: 2

Sarfaraz Nawaz
Sarfaraz Nawaz

Reputation: 361422

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++?

Upvotes: 0

John Calsbeek
John Calsbeek

Reputation: 36497

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.

Upvotes: 4

doron
doron

Reputation: 28892

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.

Upvotes: 1

Related Questions