Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

header file management practices

0 views
Skip to first unread message

ssylee

unread,
Jan 25, 2009, 12:25:33 AM1/25/09
to
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.

Juha Nieminen

unread,
Jan 25, 2009, 6:57:38 AM1/25/09
to
ssylee wrote:
> 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.

nick_keigh...@hotmail.com

unread,
Jan 25, 2009, 12:15:02 PM1/25/09
to

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.

ssylee

unread,
Jan 26, 2009, 12:35:41 PM1/26/09
to

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?

Bo Persson

unread,
Jan 26, 2009, 1:00:23 PM1/26/09
to

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

Happens all the time! :-)


Bo Persson


James Kanze

unread,
Jan 27, 2009, 3:41:07 AM1/27/09
to

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).

--
James Kanze (GABI Software) email:james...@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

0 new messages