F
Frederick Gotham
Back in the day, if you wanted a function to be self-contained within a
translation unit, you defined the function as "static".
If there were an external linkage function by the same name residing in a
different translation unit, then the current translation unit was simply
oblivious to it and had no way of accessing it. Any time the function
name was mentioned in the current translation unit, it referred to the
"static" one which resides in the current translation.
Here's an example to demonstrate what I mean:
/* a.cpp */
int Func()
{
return 10;
}
/* b.cpp */
static int Func()
{
return 5;
}
int main()
{
Func(); /* This calls the internal function */
/* We have no way of accessing the external
linkage function. */
}
Also note that you can't put a forward declaration in "b.cpp" to the
effect of:
extern int Func();
(This gives a compile error on my system when followed by a static
function of the same name and signature.)
Now that we've moved on to anonymous namespaces, it seems that things
have changed a little. We can now access the external linkage function,
but at expense of not being able to access the internal linkage one (or
so I think?)
Here's the code:
/* a.cpp */
int Func()
{
return 10;
}
/* b.cpp */
int Func(); /* Grants us access to the external one */
namespace {
int Func()
{
return 5;
}
}
int main()
{
::Func(); /* Calls the external one*/
/* I don't think we've any way of calling
the internal one. */
}
If we omit the forward-declaration of "Func" at the beginning of "b.cpp",
then of course, we only have access to the internal one, and NOT to the
external one.
Any thoughts on this?
translation unit, you defined the function as "static".
If there were an external linkage function by the same name residing in a
different translation unit, then the current translation unit was simply
oblivious to it and had no way of accessing it. Any time the function
name was mentioned in the current translation unit, it referred to the
"static" one which resides in the current translation.
Here's an example to demonstrate what I mean:
/* a.cpp */
int Func()
{
return 10;
}
/* b.cpp */
static int Func()
{
return 5;
}
int main()
{
Func(); /* This calls the internal function */
/* We have no way of accessing the external
linkage function. */
}
Also note that you can't put a forward declaration in "b.cpp" to the
effect of:
extern int Func();
(This gives a compile error on my system when followed by a static
function of the same name and signature.)
Now that we've moved on to anonymous namespaces, it seems that things
have changed a little. We can now access the external linkage function,
but at expense of not being able to access the internal linkage one (or
so I think?)
Here's the code:
/* a.cpp */
int Func()
{
return 10;
}
/* b.cpp */
int Func(); /* Grants us access to the external one */
namespace {
int Func()
{
return 5;
}
}
int main()
{
::Func(); /* Calls the external one*/
/* I don't think we've any way of calling
the internal one. */
}
If we omit the forward-declaration of "Func" at the beginning of "b.cpp",
then of course, we only have access to the internal one, and NOT to the
external one.
Any thoughts on this?