Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > static constants are not local to the file they're defined.

Reply
Thread Tools

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

 
 
Luca Cerone
Guest
Posts: n/a
 
      03-01-2012
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

 
Reply With Quote
 
 
 
 
Geoff
Guest
Posts: n/a
 
      03-01-2012
On Thu, 1 Mar 2012 00:28:44 -0800 (PST), Luca Cerone
<> wrote:

>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.
 
Reply With Quote
 
 
 
 
Luca Cerone
Guest
Posts: n/a
 
      03-01-2012
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
 
Reply With Quote
 
Luca Cerone
Guest
Posts: n/a
 
      03-02-2012
>
> 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
 
Reply With Quote
 
Luca Cerone
Guest
Posts: n/a
 
      03-02-2012
> On Friday, March 2, 2012 9:53:58 AM UTC, robert...@yahoo.com wrote:
>
> *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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Are constants not locally static? M. Norton VHDL 4 10-20-2008 09:13 PM
Static inline functions with static local variables Martin Wells C Programming 10 10-08-2007 03:38 PM
Playing a local mpeg file from a local HTML file... Lyndon HTML 1 07-25-2005 02:21 AM
Browser link to local file works when local, not work when servedfrom http lurker HTML 1 04-05-2005 05:10 AM
a static local variable in a static method is thread local storage? Patrick Hoffmann C++ 3 08-08-2003 02:37 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57