ik wrote in news:10**********************@k17g2000odb.googlegr oups.com in
comp.lang.c++:
Hello all,
Under C++ when 'extern' is used with out the string literal "C" does
it act the same as a 'static' ?
No extern is used to declare that something is defined elsewhere possibly
in another TU (TU = Translation Unit, a source file or library etc).
static has several meanings:
1) Used in a class/struct/union it declares/defines that the class
(not its instances) has a member object or function.
2) Used at namespace scope (*) or within a function it declares/defines
an object or function (namespace scope only) that has internal
linkage, i.e. other TU's (sources) will not be able to access
this object (as it doesn't have an "extern" name).
Within a function it also means the object will live from the
time it is first initialized (when the function is first called)
to the end of the programme.
*) namespace scope: within a namespace block:
namespace name { /* here */ }
or at global scope, not in a namespace, class or function.
When is it appropriate using 'extern' ?
extern is mostly useful for objects that you want to declare but
not define:
int x;
extern int y;
In the above x is declared and defined, y is only declared, so
in another TU (source) y could be defined:
int y = 2;
However both x and y have external linkage, its the default.
extern also needs to be used with const's that you want to have
external linkage, as const object's have internal linkage by
default (i.e. they are static):
int const a = 3;
extern int const b;
extern int const c = 4;
In the above a has internal linkage, b has external linkage
and will need to be defined elsewhere:
extern int const b = 2;
and c has external linkage, but because it has an intializer
it is also a defenition.
Declaration's are extern by default so, with function's:
int f();
is equivalent to:
extern int f();
The only time you need to use extern with a function is
when you want to give it a linkage specification:
extern "C" int g();
HTH.
Rob.
--
http://www.victim-prime.dsl.pipex.com/