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.

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

  2. Luca Cerone

    Geoff Guest

    Some functions... what? Definitions? How do you include this file in
    multiple places and not get linker errors then?
    Are you sure?
    Possible your compiler is broken?

    Post the complete sources of all three files listed above.
    Geoff, Mar 1, 2012
    1. Advertisements

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

    Luca Cerone Guest

    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?
    Thanks (to Christian as well), now I have clear the difference :)
    Luca Cerone, Mar 2, 2012
  5. Luca Cerone

    Luca Cerone Guest

    Got it.
    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 Cerone, Mar 2, 2012
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.