static constants are not local to the file they're defined.

L

Luca Cerone

Dear all,
I'm a bit confused from the use of static to declare constants that
can be used only by the file they are defined in.

I've made a small test using a main.cpp a mylib.h and a mylib.cpp creating the files like this:

//main.cpp ===============================
#include <iostream>
using namespace std;
#include "mylib.h" ;

int main(){
//L_COS is the constant that I only want to be usable by mylib.cpp
cout << "Hello, World!" << L_COS << endl;

return 0;
}

//mylib.h ===========================

// nothing relevant here, some functions...
// NO declaration of L_COS here.

//mylib.cpp
#include "mylib.h"

static const double L_COS= 1.2345 //my constant.
//I'd like L_COS to be used by the functions in mylib.cpp
//but not in the main.cpp file.
//===========================================

I'd expect that main.cpp doesn't compile. Not only it compiles,
but also print the correct value of L_COS.

So the question is: how can I make L_COS global for all the functions
of mylib.cpp, but not in the scope of any other files?

Thanks a lot in advance,
Luca
 
G

Geoff

Dear all,
I'm a bit confused from the use of static to declare constants that
can be used only by the file they are defined in.

I've made a small test using a main.cpp a mylib.h and a mylib.cpp creating the files like this:

//main.cpp ===============================
#include <iostream>
using namespace std;
#include "mylib.h" ;

int main(){
//L_COS is the constant that I only want to be usable by mylib.cpp
cout << "Hello, World!" << L_COS << endl;

return 0;
}

//mylib.h ===========================

// nothing relevant here, some functions...

Some functions... what? Definitions? How do you include this file in
multiple places and not get linker errors then?
// NO declaration of L_COS here.

Are you sure?
//mylib.cpp
#include "mylib.h"

static const double L_COS= 1.2345 //my constant.
//I'd like L_COS to be used by the functions in mylib.cpp
//but not in the main.cpp file.
//===========================================

I'd expect that main.cpp doesn't compile. Not only it compiles,
but also print the correct value of L_COS.

So the question is: how can I make L_COS global for all the functions
of mylib.cpp, but not in the scope of any other files?

Possible your compiler is broken?

Post the complete sources of all three files listed above.
 
L

Luca Cerone

Thanks for your replies.
Apparently I made something wrong before because creating
a minimal example to post here I could use the declared constant only
in mylib.cpp.

What I don't get though is: why should I declare it static?
If I declare a simple const double variable I get the same a variable
that is only valid in mylib.cpp and not in the other files.

What does "static" actually do when declaring a global variable in mylib.cpp?
Can you post me some code where it does make a difference using static or not?
(as global variable, I get what static does inside the scope of a function).

Thanks for your help,
Luca
 
L

Luca Cerone

Please quote properly. This is Usenet, not a web forum.
Sorry I didn't mean to do something wrong. As you'd have guessed I'm a newbie
to groups and programming, so I'm sorry if I did something wrong.
Where can I find guidelines to post in this group?
If you don't define the item static, you could reference it from
another translation unit with an "extern double const L_COS;" Also,
you *couldn't* define another (non-static) L_COS in another
translation unit in the same program, since the external names will
then conflict.

Thanks (to Christian as well), now I have clear the difference :)
 
L

Luca Cerone

*Always* include the attribution lines, so we can figure
out who's saying what and when (you trimmed those in your response -
see mine above). Always trim quoted material no longer relevant.

Got it.
In general, don't post the same message to multiple groups (*never*
post the same message separately to multiple groups, if you must post
to more than one group, post the one message to multiple destination
groups).

Read the group's FAQ, and spend a bit of time watching the group
before posting, to make sure you're in the right spot, and asking a
question appropriate to the group.

I'm sorry but accessing this from Google Groups I couldn't see any
FAQ page nor group etiquette (that seemed strange to me. With the old interface there was a specific link to the group description and to the group FAQ, but not since they changed it.)
Also, I don't know if Google Groups allows you to post to more than one group
at once.. so I have no idea how to do that other than copy and paste the
same message on different groups.

I'm very sorry to have created this OT.

Thanks again for the advices on c++ and on the group!
Luca
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,955
Messages
2,570,117
Members
46,705
Latest member
v_darius

Latest Threads

Top