|
-
September 18th, 2002, 03:30 AM
#1
Extern variable problem
Can anyone tell me if I can do the following:
Define a global variable in one cpp/c file (call it file1.c)
Then have it as an extern in a second file (call it file2.c),
but don't allow it to be changed from this file.
for example:
/****file1.c********/
double NiceVar;
void function()
{
NiceVar = 100; // I can set the value in file1.c easily.
}
/****************/
/****file2.c********/
extern double NiceVar;
void main()
{
NiceVar = 1000; // This shouldn't work.
printf( "%f", NiceVar ) // But this should return 100
}
/****************/
You'll have to excuse the basic explanation, but I am still a beginner!
-
September 18th, 2002, 03:59 AM
#2
Code:
// file 1.cpp
double niceVar;
// file1.h (or file2.cpp)
extern const double niceVar;
Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
-- Sutter and Alexandrescu, C++ Coding Standards
Programs must be written for people to read, and only incidentally for machines to execute.
-- Harold Abelson and Gerald Jay Sussman
The cheapest, fastest and most reliable components of a computer system are those that aren't there.
-- Gordon Bell
-
September 19th, 2002, 03:32 AM
#3
Thanks for your help Graham. I've tried this but I am still
having problems, however I think it may be a compiler related issue. I've compiled this code using MS Visual C++ and it gives me the following error:
ile2.obj : error LNK2001: unresolved external symbol "double const NiceVar" (?NiceVar@@3NB)
Debug/Code1.exe : fatal error LNK1120: 1 unresolved externals
I've also compiled it using a Unix version of GCC and it seems to link fine.
Any ideas?
-
September 19th, 2002, 06:06 AM
#4
Are you sure both files have a *.c extension? The compiler does not mangle the names of objects in a *.c file, but it does in *.cpp files. In a *.cpp file you should declare it like this:
Code:
extern "C" double NiceVar;
-
September 19th, 2002, 08:27 AM
#5
I am actually using cpp files stober, but the fix you mentioned
seems to give a different error. The code is thus:
/****file1.cpp********/
#include "stdio.h"
double NiceVar;
void function()
{
NiceVar = 100; // I can set the value in file1.cpp easily.
}
/****************/
/****file2.cpp********/
#include "stdio.h"
extern "C" const double NiceVar;
void main()
{
//NiceVar = 1000; // This shouldn't work.
printf( "%f", NiceVar ); // But this should return 100
}
/****************/
When I compile in Vis C++ I get:
Linking...
file2.obj : error LNK2001: unresolved external symbol _NiceVar
Debug/Code1.exe : fatal error LNK1120: 1 unresolved externals
Error executing link.exe.
Sorry to keep going on about this, but its one if those annoying things where you can't go any further on a project until you've got it sorted!
-
September 19th, 2002, 09:45 AM
#6
Obviously the answer given by Graham would not work. A value declared as const or none-const have different storage requirements. Const values are either resolved inline or stored in a read-only section of memory. None const values are stored in a writable memory section. So if you declare it as const in one file and none const in another file, it will not be resolved.
If the global value you declared is changeable in one file, there is really nothing that can prevent another file from changing it. But there may be ways to aviod the second file from changing it accidentally or unintentionally.
Declare the global valuable as static in the file where it is defined. That way other files would not be able to see it, althrough they can still change or access it had they know the address. Next, declare a global const pointer which points to the address of your global variable. Expose this const pointer to other files. This way other files can access the variable through this const pointer. But they can not change the value since it is a const pointer.
Those files that wants to change the value of the global variable can do so by explicitly casting the const pointer to a none-const pointer, and then they can use the none-const pointer to modify the global variable.
Certainly if any file want to modify the global variable, they can do the const-to-none-const casting too and gain access. The point is hopefully the extra steps would prevent accidental and un-intentional modification. As for intensional access, nothing can be done to prevent that.
-
September 19th, 2002, 10:37 AM
#7
You can also use references as a variant on Anthony's suggestion.
Code:
// file 1.cpp
double horribleVar;
const double& niceVar = horribleVar;
// file1.h (or file2.cpp)
extern const double& niceVar;
Succinct is verbose for terse
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
|