Static variables and header files

This is a discussion on Static variables and header files within the C++ Programming forums, part of the General Programming Boards category; I was wondering if there's a way around having to declare static variables only once when writing classes which are ...

  1. #1
    Registered User
    Join Date
    Oct 2005
    Posts
    88

    Static variables and header files

    I was wondering if there's a way around having to declare static variables only once when writing classes which are just in header files. I didn't think it could be done, but looking around the boost libraries, I've noticed that there are some libs which are entirely contained in header files but still seem to have static members. You can't just declare it outside of the class can you? Including the file in different translation units means a multiply defined static object, no?

  2. #2
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,475
    This may not be what you want, but you can use the keyword "extern" in this case

    header file

    Code:
    class Foo
    {
    public:
       Foo();
       ~Foo();
    
       static int s_value;
    };
    
    extern Foo fo; // object
    .cpp file

    Code:
    #include "foo.h"
    
    Foo fo; // declares this class object can be used elsewhere
    
    int Foo::s_value = 5;
    Please ignore this if it is not what you want. I was trying to understand what you were
    looking for. In general, delcare static members outside the class in the .cpp header refering to the header class defintion file.
    I'm just trying to be a better person - My Name Is Earl

  3. #3
    Registered User
    Join Date
    Oct 2005
    Posts
    88
    Thanks for the reply, but what you're saying to do is the only way I know to do that. The thing I'm wondering is how you implement the declaration of, in your example, Foo::s_value without having a .cpp file. It must be possible but I can't figure out how. Any ideas?

  4. #4
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,475
    You can do this:

    Code:
    int Foo::s_value = 5
    in the header file which contains that class. Just inialize the data member with the value
    under the class. I personally think it goes against the idea of seperating declaration from
    implementation. But that is just my view, it is by no means an absolute
    I'm just trying to be a better person - My Name Is Earl

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    >> looking around the boost libraries, I've noticed that there are some libs which are entirely contained in header files but still seem to have static members.

    Do you have an example? Do you have your own small example of what you are trying to do?

  6. #6
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,406
    > I've noticed that there are some libs which are entirely contained in header files but still seem to have static members.

    Are you sure you aren't looking at const static members?
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  7. #7
    Registered User
    Join Date
    Oct 2005
    Posts
    88
    Sorry about the late reply.

    The idea does go against seperation of code, but I prefer the idea of having a library that doesn't need to be linked with to work. It's more just for learning though. The example which made me think I could do this was Boost.Asio. If you look here, the class has static member functions which seem like they have to be non-const (I could be wrong though). I don't see how you can declare a static function/variable without having it in an isolated file. Can you?

  8. #8
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,406
    Well, to start with static member functions cannot be const. Since they are a part of the class and not of the object, they cannot promise to not alter the object. It wouldn't make sense... they aren't aware of the object. They aren't passed the This pointer as non-static member functions are.

    Those static member functions on asio's address class are special. The key to understanding them is to look at the return type of those functions.

    They are declared as members of address and return an address object. They are made static in order to be able to be used without the need to instantiate an object. From the return type (and hinted from the documentation), these seem to be functions that create instances of the address class.

    Lets take static from_string (const char *str) as an example. From the documentation it promises to "Create an address from an IPv4 address string in dotted decimal form, or from an IPv6 address in hexadecimal notation."

    How would you do this without using a static member function? Well, you could make a non member function and declare it inside some address namespace. That function would then basically do the same as the current one do. Create an instance of address class and return it.

    By making it a member function, the function is automatically defined in the scope of the address class. Meawnhile, despite being declared static, there are no static data members involved.
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  9. #9
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    >> The example which made me think I could do this was Boost.Asio.
    That uses static member functions. Static member functions are different than static member variables. You can implement a static member function inline. Static member variables are more of a problem, because there is no "inline" for variables.

    Do you have your own small example of what you are trying to do?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. static variables and includes?
    By Devils Child in forum C++ Programming
    Replies: 68
    Last Post: 01-30-2008, 09:59 AM
  2. Variables in header files
    By Overlord in forum C++ Programming
    Replies: 5
    Last Post: 10-15-2006, 06:43 AM
  3. What /what not to put in header files
    By Just in forum C Programming
    Replies: 1
    Last Post: 12-14-2002, 09:45 AM
  4. variables across files
    By Hunter2 in forum C++ Programming
    Replies: 13
    Last Post: 07-23-2002, 10:58 AM
  5. Header files
    By borland_man in forum C++ Programming
    Replies: 14
    Last Post: 02-22-2002, 03:30 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21