[Cs3] Extern question
Mikhail Nesterenko
mikhail at cs.kent.edu
Mon Sep 14 21:58:58 EDT 2015
Garnica, Ramsey wrote:
> Hello Professor,
>
>
> I was looking up examples of extern and one person made an analogy
> that I was wondering if you thought was a good analogy or not.
>
>
> Everybody was comparing extern to a global variable in a header file
> and one person made the analogy that prototypes are like extern
> statements for functions. That seems like a correct analogy to me
> so I was wondering if that is a correct way to think about externs?
>
It is correct in the sense that both prototypes for functions and
externs for global variables are declarations rather than definitions.
A good use of extern is this:
header1.h
---
extern int i; // tells compiler that global variable "i" is defined somewhere
---
source1.cpp
---
#include "header1.h"
int i=0; // declaration and even initialization of global variable "i"
---
source2.cpp
---
#include "header1.h"
int myfunc(){
cout << i; // usage of global variable, the compiler knows
// about "i" from header's declaration
}
---
More information about the cs3
mailing list