|
-
March 23rd, 2004, 04:24 AM
#1
"extern" declaration
I'm not sure where should I declare my extern variable. let say is
extern char nbuffer[30];
nbuffer is a global variable, and defined in other source file.
Assuming I have MainDlg.cpp, Dialog1.cpp.
In my Dialog1.cpp
#include "MainDlg.h"
.....
void CDialog1::OnMyok()
{
//here I want to use global variable nbuffer, but got error!!!
GetDlgItemText(IDC_EditBox, nbuffer, 30);
CDialog::OnOK();
}
Then I declared nbuffer at MainDlg.h after the statement, not if it is right way to do.
//{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations immediately before the previous line.
#endif //
extern char nbuffer[30];
But compilation gives me error "nbuffer" Undeclared identifier. Why ? and how to fix it ?
-
March 23rd, 2004, 04:35 AM
#2
Re: "extern" declaration
Originally posted by Dimension
I'm not sure where should I declare my extern variable. let say is
extern char nbuffer[30];
nbuffer is a global variable, and defined in other source file.
Assuming I have MainDlg.cpp, Dialog1.cpp.
In my Dialog1.cpp
#include "MainDlg.h"
.....
void CDialog1::OnMyok()
{
//here I want to use global variable nbuffer, but got error!!!
GetDlgItemText(IDC_EditBox, nbuffer, 30);
CDialog::OnOK();
}
Then I declared nbuffer at MainDlg.h after the statement, not if it is right way to do.
//{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations immediately before the previous line.
#endif //
extern char nbuffer[30];
But compilation gives me error "nbuffer" Undeclared identifier. Why ? and how to fix it ?
DO NOT use global variables. Use "static" variables instead. Search this site for discussions regarding this topic.
-
March 23rd, 2004, 04:57 AM
#3
at least, pls tell me why can't I used the "extern" global variable ?
Is that any possibility of using it as extern ? If yes, how.
Thanks
-
March 23rd, 2004, 07:32 AM
#4
Sure you can. But do it in a proper way.
The point is that extern declaration tells to compiler that the symbol has an external linkage. It means the variable shoul be defined in some other unit and its real address could be evaluated at linktime.
Code:
// unit 1 - file1.cpp
// it's a definition of a variable
// compiler interprets it as an instruction to allocate a memory
// block of particular size
char nbuffer[30];
. . .
// unit 2 - file2.cpp
// it's a declaration of variable for this unit
// compiler interprets it as an obligation that the real address
// will be inserted by linker at linktime
extern char nbuffer[30];
. . .
The basic C/C++ rule - before been used a variable should be declared in unit. So every unit should contain all declarations of all variables it used in.
Compiler uses a definition of undeclared variable as its declaration as well. But it never uses a variable declaration as its definition, because it could lead to multiple variable definition. BTW on this reason you never should place a definition (of function or variable) into header files.
The topic is too important, big and complex to be covered in forum chat. Get the book on C (classically C was more detailed covering this questions) and read the topic through (about extern and static, storage classes, definitions and declarations)
Best regards,
Igor
-
March 23rd, 2004, 11:55 AM
#5
Also, if the variable is truly global, i.e. meant to be used by all the classes in the app, then have a fxn in your app class like getBuffer() which will return a pointer or a const pointer to the buffer. All classes could then get a pointer to the app, call the fxn, and check for a NULL (error) return from this fxn. If you change something in the design of your app that affects this buffer, you can easily make the change in that one function alone, instead of going thru all your files. While this is not much of an issue if you only have 4 or 5 source files, it can become a real pain in the butt if you have 50, 80 or more source files, and it's a good habit to get into.
Steve
-
March 23rd, 2004, 02:21 PM
#6
It is almost always possible to do whatever you want without global variables...
If it is not, it is usyally a good practice not to use extern to access an "extern" variable, but have functions to access it (set/get).
This can allow you to implement semaphores or hatever synchronization method you want in case of problems or further development.
However if you are completly sure that your variable won´t be accessed by two functions at a time or you don´t need any kind of synchronization you can use extern.
Using it is very simple... You have a variable that is used in more than one module / file... In one of the you declare it normal and in the rest with extern at the beginning... This will tell the linker that the variable you are refering in both files is the same and is linked in the same address in memory.
Just one more comment... declaring a variable in a *.h is not recommended at all.
Caronte
Si tiene solución... ¿por qué te preocupas?
Si no tiene solución... ¿por qué te preocupas?
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|