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

Discussion in 'C++' started by Luca Cerone, Mar 1, 2012.

  1. Luca Cerone

    Luca Cerone Guest

    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
    Luca Cerone, Mar 1, 2012
    #1
    1. Advertising

  2. Luca Cerone

    Geoff Guest

    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.
    Geoff, Mar 1, 2012
    #2
    1. Advertising

  3. Luca Cerone

    Luca Cerone Guest

    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
    Luca Cerone, Mar 1, 2012
    #3
  4. Luca Cerone

    Luca Cerone Guest

    >
    > 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 :)
    Luca Cerone, Mar 2, 2012
    #4
  5. Luca Cerone

    Luca Cerone Guest

    > On Friday, March 2, 2012 9:53:58 AM UTC, 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
    Luca Cerone, Mar 2, 2012
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Patrick Hoffmann
    Replies:
    3
    Views:
    2,816
    Christian Jan├čen
    Aug 8, 2003
  2. Oodini
    Replies:
    1
    Views:
    1,754
    Keith Thompson
    Sep 27, 2005
  3. neilmac
    Replies:
    2
    Views:
    636
    mcdjnaja
    Feb 7, 2008
  4. Paul Brannan
    Replies:
    13
    Views:
    284
    Paul Brannan
    Mar 4, 2008
  5. js
    Replies:
    2
    Views:
    91
Loading...

Share This Page