header file management practices

S

ssylee

For example, I have two .cpp files. One called a.cpp and b.cpp.

a.cpp contains the function definition for the following functions:

void a_fun1(...)
{
....
}


void a_fun2(...)
{
....
}

and b.cpp contains function definition for the following functions:

void b_func1(...)
{
....
}

void b_func2(...)
{
....
}

Let's say in the implementation of b_func1() function, I need to use a
function in a.cpp (e.g. a_fun2() function). I know that I can expose
the functions in a.cpp that I want to the public in the project by
declaring the functions in the header file. I also know that I can
declare an extern function in b.cpp before calling that particular
function. What are the pros and cons of each? It would be great to
hear about some second opinions. The only ones that I can think of are
exposing the functions via the header file would make the project
easier to manage, while I would get rid of extra header files if I
declare the functions to be extern before they are used.
 
J

Juha Nieminen

ssylee said:
Let's say in the implementation of b_func1() function, I need to use a
function in a.cpp (e.g. a_fun2() function). I know that I can expose
the functions in a.cpp that I want to the public in the project by
declaring the functions in the header file.

Actually your functions, by being global (ie. not static nor inside a
nameless namespace), are *already* public, ie. in the global namespace.
This means any code could call them if they know their name and
signature, even without that header file.

Putting the function declarations in a header file does absolutely
nothing with respect to how "visible" those functions are to the
outside. By being global, they already are fully visible to the outside.
The only thing the header file would do is to make it easier for outside
code to call your functions (but, as I said, it's still possible to call
the functions even without any header file). Without the header file the
outside code could simple declare the functions themselves (with the
proper name and signature) and call them. It would work exactly as if
you had provided the header file.

The only way to make the functions not accessible from the outside is
to make them static (the C way) or put them inside a nameless namespace
(the C++ way). (Note that you can't make them public by anything you put
in a header file.)
I also know that I can
declare an extern function in b.cpp before calling that particular
function.

Function declarations are implicitly 'extern' without having to
explicitly write that keyword (but you can write it if you want).
What are the pros and cons of each? It would be great to
hear about some second opinions. The only ones that I can think of are
exposing the functions via the header file would make the project
easier to manage, while I would get rid of extra header files if I
declare the functions to be extern before they are used.

The header file would only be a convenience. It would not change the
visibility of the functions.
 
G

Guest

  Actually your functions, by being global (ie. not static nor inside a
nameless namespace), are *already* public, ie. in the global namespace.
This means any code could call them if they know their name and
signature, even without that header file.

  Putting the function declarations in a header file does absolutely
nothing with respect to how "visible" those functions are to the
outside. By being global, they already are fully visible to the outside.
The only thing the header file would do is to make it easier for outside
code to call your functions (but, as I said, it's still possible to call
the functions even without any header file). Without the header file the
outside code could simple declare the functions themselves (with the
proper name and signature) and call them. It would work exactly as if
you had provided the header file.

  The only way to make the functions not accessible from the outside is
to make them static (the C way) or put them inside a nameless namespace
(the C++ way). (Note that you can't make them public by anything you put
in a header file.)


  Function declarations are implicitly 'extern' without having to
explicitly write that keyword (but you can write it if you want).


  The header file would only be a convenience. It would not change the
visibility of the functions.

I'd argue the header file is the better technique.
If the prototype changes then you only have to
change the header rather than seacrh all the source
for the embedded declarations.
 
S

ssylee

I'd argue the header file is the better technique.
If the prototype changes then you only have to
change the header rather than seacrh all the source
for the embedded declarations.

Thanks for all your replies. If a particular function is only used
once in another cpp file, and declared an extern in the cpp file using
the function, would it make a difference anyway in terms of ease of
managing the project?
 
B

Bo Persson

ssylee said:
Thanks for all your replies. If a particular function is only used
once in another cpp file, and declared an extern in the cpp file
using the function, would it make a difference anyway in terms of
ease of managing the project?

It would, the day you realize that the function can also be used in a
third cpp file.

Happens all the time! :)


Bo Persson
 
J

James Kanze

It would, the day you realize that the function can also be
used in a third cpp file.
Happens all the time! :)

More generally, the usual practice is to include the header in
the file that defines the function as well. So the compiler can
control various incompatibilities (e.g. if you modify the return
type).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top