Advertisement

Why would someone want to do this?

Started by July 09, 2000 06:37 AM
10 comments, last by CrazyIvan 24 years, 5 months ago
Hi, in my quest to become a real good programmer i started reading the online version of Thinking in C++. I saw a little (the first actually) code example..

//: C02:Declare.cpp
// Declaration & definition examples
extern int i; // Declaration without definition
extern float f(float); // Function declaration
float b; // Declaration & definition
float f(float a) { // Definition
return a + 1.0;
}
int i; // Definition
int h(int x) { // Declaration & definition
return x + 1;
}
int main() {
b = 1.0;
i = 2;
f(b);
h(i);
} ///:~
 
My question is why would someone want to use extern int i; (the declaration) and later on just use int i; (the definition)? If this is the wrong forum for a newbie question please let me know i''m new to these forums
And another thing... why do i see that a certain post has 0 or 1 replies but when i open it it has 6 or 7?
I heared about people having this error on some other messageboard but i never had it.

And third is there a way to post code and keep the tabs?

Edited by - CrazyIvan on July 9, 2000 7:39:20 AM
Advertisement
       extern int i;      void main(void)   {      int i = 2;      i += ::i; // access the global scope variable i      printf("%i", i);   }    


ps. Use [ source ] and [ /source ] - without the spaces of course.

"Paranoia is the belief in a hidden order behind the visible." - Anonymous
extern is used to let the compiler know that there is a global variable declared in another file.
if you use "extern int i;" without ever declaring "int i;" your compiler will give you an error becuase you told it that something is there which actually isn''t. There should only be 1 declaration of a global variable, and anything that needs to use it but cant directly see the declaration (ie is in a different file), you can use extern to let the compiler know that it is there.


JS
http://www.thepeel.com/void
JShttp://www.thepeel.com/void
The reason the number of replies is often wrong is cos there is a bug in the board software

Just my thoughts take them as you will. "People spend too much time thinking about the past, whatever else it is, its gone"-Mel Gibson, Man Without A Face
And I thought it was a feature...

MK42
Advertisement
It''s been a while since I read his book (cant'' remember the author, Bruce somthing I think) As I recall, He was giving an example of function overloading. The code snipette you found was part of a discussion he was developing to show why function overloading is usefull. The code in itself is a silly example that doesn''t do anything usefull.

John G.
Perhaps the developers who wrote the board should post the code so we can fix it for them...
quote: Original post by Zeke

The reason the number of replies is often wrong is cos there is a bug in the board software



Actually, my understanding is it''s the # of replies since your last visit to the forum.
Maybe not.

B e S
It's Da BOMB Baby!!!
BeSIt's Da BOMB Baby!!!. o O ~ A little nonsense now and then,is relished by the wisest men~ O o .-- Willy Wonka
I think the reason they used "extern" in this case is to illustrate the difference between "static" and "extern" for global variables. Having

extern int i;
int i;

is exactly the same as

int i;

However,

static int i;

is different because you won''t be able to access i in a different module. "static" and "extern" are storage class modifiers, and "extern" is implicity implied for globals by default. It was probably explicitly put in to make things "clearer". Or, its simlpy possible that this is the (somewhat unusual) programming style the author uses.
Volition, Inc.

This topic is closed to new replies.

Advertisement