472,328 Members | 1,596 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,328 software developers and data experts.

extern and static

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

Jul 22 '05 #1
2 2272
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
Jul 22 '05 #2
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/
Jul 22 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Mark A. Gibbs | last post by:
I have a question about mixing C and C++. In a C++ translation unit, I want to define a function with internal linkage and C calling convention....
18
by: tweak | last post by:
What's the best way to use extern when using multiplefiles that is easiest to maintain? Is it best to declare: extern int a; in a header...
19
by: ccwork | last post by:
Hi all, I am reading "C: A Reference Manual" 4th ed and I get lost for the "extern". It says that global object without specifying the...
17
by: Tapeesh | last post by:
I would like to know what is the expected behaviour of C compilers when an extern decleration is intialized. When the following code is compiled...
4
by: Sean | last post by:
I am a little confused by the "extern inline and static inline" rules. I understand that "extern inline" guarantees no function storage is created...
5
by: Christian Christmann | last post by:
Hi, I've tree questions on the storage class specifier "extern": 1) Code example: int main( void ) { int b = -2; // my line 3 if ( a ) {
7
by: ruffiano | last post by:
I have a file called first.cpp that contains the following declarations: static const char *MY_STRING1 = "My first string"; static const char...
3
by: coder | last post by:
While reading the page on "Reading C Declarations" <http://www.ericgiguere.com/articles/reading-c-declarations.html> (recommended by Mr. Heathfield...
5
by: Anonymous | last post by:
I have a class that needs to be accesed by a C API. I need to expose some private methods to the C API : #ifdef __cplusplus extern "C" #endif...
0
by: concettolabs | last post by:
In today's business world, businesses are increasingly turning to PowerApps to develop custom business applications. PowerApps is a powerful tool...
0
better678
by: better678 | last post by:
Question: Discuss your understanding of the Java platform. Is the statement "Java is interpreted" correct? Answer: Java is an object-oriented...
0
by: teenabhardwaj | last post by:
How would one discover a valid source for learning news, comfort, and help for engineering designs? Covering through piles of books takes a lot of...
0
by: CD Tom | last post by:
This happens in runtime 2013 and 2016. When a report is run and then closed a toolbar shows up and the only way to get it to go away is to right...
0
by: CD Tom | last post by:
This only shows up in access runtime. When a user select a report from my report menu when they close the report they get a menu I've called Add-ins...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was...
0
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. ...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.