Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c++ > #14639 > unrolled thread
| Started by | Luca Cerone <luca.cerone@gmail.com> |
|---|---|
| First post | 2012-03-01 00:28 -0800 |
| Last post | 2012-03-02 02:42 -0800 |
| Articles | 9 — 4 participants |
Back to article view | Back to comp.lang.c++
static constants are not local to the file they're defined. Luca Cerone <luca.cerone@gmail.com> - 2012-03-01 00:28 -0800
Re: static constants are not local to the file they're defined. Robert Wessel <robertwessel2@yahoo.com> - 2012-03-01 02:55 -0600
Re: static constants are not local to the file they're defined. Geoff <geoff@invalid.invalid> - 2012-03-01 01:25 -0800
Re: static constants are not local to the file they're defined. Luca Cerone <luca.cerone@gmail.com> - 2012-03-01 06:51 -0800
Re: static constants are not local to the file they're defined. Christian Gollwitzer <auriocus@gmx.de> - 2012-03-01 19:21 +0100
Re: static constants are not local to the file they're defined. Robert Wessel <robertwessel2@yahoo.com> - 2012-03-01 12:21 -0600
Re: static constants are not local to the file they're defined. Luca Cerone <luca.cerone@gmail.com> - 2012-03-02 00:56 -0800
Re: static constants are not local to the file they're defined. Robert Wessel <robertwessel2@yahoo.com> - 2012-03-02 03:53 -0600
Re: static constants are not local to the file they're defined. Luca Cerone <luca.cerone@gmail.com> - 2012-03-02 02:42 -0800
| From | Luca Cerone <luca.cerone@gmail.com> |
|---|---|
| Date | 2012-03-01 00:28 -0800 |
| Subject | static constants are not local to the file they're defined. |
| Message-ID | <16464242.82.1330590524680.JavaMail.geo-discussion-forums@vbtf25> |
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
[toc] | [next] | [standalone]
| From | Robert Wessel <robertwessel2@yahoo.com> |
|---|---|
| Date | 2012-03-01 02:55 -0600 |
| Message-ID | <m9euk7tovkvarrfiira1pbhrq3c3qpcs5i@4ax.com> |
| In reply to | #14639 |
On Thu, 1 Mar 2012 00:28:44 -0800 (PST), Luca Cerone
<luca.cerone@gmail.com> 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...
>// 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?
Just define it in mylib.cpp, not in mylib.h. When you put the
definition in mylib.h, it's included anywhere where you include
mylib.h.
[toc] | [prev] | [next] | [standalone]
| From | Geoff <geoff@invalid.invalid> |
|---|---|
| Date | 2012-03-01 01:25 -0800 |
| Message-ID | <vufuk7p2jhp5ffu33d75csjgpj9bkm7634@4ax.com> |
| In reply to | #14639 |
On Thu, 1 Mar 2012 00:28:44 -0800 (PST), Luca Cerone
<luca.cerone@gmail.com> 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.
[toc] | [prev] | [next] | [standalone]
| From | Luca Cerone <luca.cerone@gmail.com> |
|---|---|
| Date | 2012-03-01 06:51 -0800 |
| Message-ID | <17386272.1266.1330613495577.JavaMail.geo-discussion-forums@vbux23> |
| In reply to | #14639 |
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
[toc] | [prev] | [next] | [standalone]
| From | Christian Gollwitzer <auriocus@gmx.de> |
|---|---|
| Date | 2012-03-01 19:21 +0100 |
| Message-ID | <jioen5$4ct$1@dont-email.me> |
| In reply to | #14652 |
Am 01.03.12 15:51, schrieb 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. Invisible to cpp files, but not invisible to the linker. With a usual const variable, all that neds to be done by another cpp file is to do extern const double L_COS; to access it. If L_COS is declared static, it can not be accessed from outside. Additionally, you could have multiple different L_COS variables in different translation units (cpp files) Christian
[toc] | [prev] | [next] | [standalone]
| From | Robert Wessel <robertwessel2@yahoo.com> |
|---|---|
| Date | 2012-03-01 12:21 -0600 |
| Message-ID | <b8fvk7lp0qgjctfusfbrvv2rhhn6ebe6do@4ax.com> |
| In reply to | #14652 |
On Thu, 1 Mar 2012 06:51:35 -0800 (PST), Luca Cerone <luca.cerone@gmail.com> wrote: >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). Please quote properly. This is Usenet, not a web forum. 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.
[toc] | [prev] | [next] | [standalone]
| From | Luca Cerone <luca.cerone@gmail.com> |
|---|---|
| Date | 2012-03-02 00:56 -0800 |
| Message-ID | <11063471.45.1330678603134.JavaMail.geo-discussion-forums@vbne13> |
| In reply to | #14659 |
> > 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 :)
[toc] | [prev] | [next] | [standalone]
| From | Robert Wessel <robertwessel2@yahoo.com> |
|---|---|
| Date | 2012-03-02 03:53 -0600 |
| Message-ID | <k751l7lotv08lmk9snkm1ce8b6vmvdet3n@4ax.com> |
| In reply to | #14678 |
On Fri, 2 Mar 2012 00:56:42 -0800 (PST), Luca Cerone <luca.cerone@gmail.com> wrote: >> >> 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? It's pretty informal, but since this isn't a web forum, you need to quote what you're responding to. Often we cannot see the prior message. Don't top post. Always post at the end, or inline (as you did here). *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. 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. For example, this is a C++ group - questions about the Windows or Unix API, or how to convince Visual Studio to compile your program, are *not* topical here, even if you're writing your program in C++. DON'T SHOUT. Beyond that, be a reasonable human being, and you're 95% of the way there. But Google for "usenet etiquette", for tons more commentary.
[toc] | [prev] | [next] | [standalone]
| From | Luca Cerone <luca.cerone@gmail.com> |
|---|---|
| Date | 2012-03-02 02:42 -0800 |
| Message-ID | <19188065.600.1330684924424.JavaMail.geo-discussion-forums@vbxv4> |
| In reply to | #14681 |
> 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
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.c++
csiph-web