extern and static

I

ik

Hello all,
Under C++ when 'extern' is used with out the string literal "C" does
it act the same as a 'static' ? When is it appropriate using 'extern' ?
Thanks
~Ik
 
J

JKop

ik posted:
Hello all,
Under C++ when 'extern' is used with out the string literal "C" does
it act the same as a 'static' ? When is it appropriate using 'extern' ?
Thanks
~Ik

When you write:

int k;

Then that's going to define a variable.

But what if you don't want to define a variable, what if
this variable has already been defined in some other source
file and you just want to use it in this source file. Well
here's how you say that you *don't* want to define a
variable, you just want to use one that's already been
defined (sort of like a function prototype):

extern int k;


-JKop
 
R

Rob Williscroft

ik wrote in 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.
 

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

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top