tricky: static int on template class;

This is a discussion on tricky: static int on template class; within the C++ Programming forums, part of the General Programming Boards category; Code: //MyTemplate template<class T> class MyTemplate { public: static int counter; }; Code: //main int main(void) { MyTemplate<int> i; MyTemplate<float> ...

  1. #1
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297

    tricky: static int on template class;

    Code:
    //MyTemplate
    template<class T>
    class MyTemplate
       {
       public:
          static int counter;
       };
    Code:
    //main
    int main(void)
       {
       MyTemplate<int> i;
       MyTemplate<float> f;
       return 1;
       }
    It seems obvious that there are TWO static counter's here, MyTemplate<int>::counter and MyTemplate<float>::counter

    With that in mind, I have two questions

    1. is it possible to create a static across ALL MyTemplate's?

    2. Where would the following line go? and would there be any problems with putting it there that you can see?
    int MyTemplate<T>::counter = 0;
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  2. #2
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,789

    Re: tricky: static int on template class;

    Code:
    #include <iostream>
    
    class MultiCounter
    {
    public:
    	static int multicounter;
    };
    	
    int MultiCounter::multicounter;
    
    template<class T>
    class MyTemplate : public MultiCounter
    {
    public:
    	static int counter;
    };
    
    template<class T>
    int MyTemplate<T>::counter;
    
    int main(void){
    
       std::cout << ++(MyTemplate<int>::counter) << std::endl;
       std::cout << ++(MyTemplate<float>::counter) << std::endl;
       
       //but
       
       std::cout << ++(MyTemplate<int>::multicounter) << std::endl;
       std::cout << ++(MyTemplate<float>::multicounter) << std::endl;
    
    }
    As you have probably guessed, MyTemplate<int> and MyTemplate<float> are completely different classes when compiled....so defineing a static varibale wont apply to both of those types......as normal though - there is a way around - derive the template from a non template class and hold your static member there

    You define counter outside the class declaration as you would any static member...define it as above and you wont have a problem

    Templates - tricky buggers

  3. #3
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Fordy is right on the money (which is not unusual). You could also make a static global variable as your counter. The global should be a little more desirable unless there is something you are trying to do as far as encapsulation is concerned.

  4. #4
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    ah, but the real question is #2. I actually want to have two separate ones. The reason is too complex to go into in this thread but it has to do with a cross platform windowing template hierarchy that I'm working on.

    So assuming that a given class is the template parameter, I'm having to put the following line in the cpp for that class:

    int Window<ClassName>::s_instancecount= 0;

    I would prefer not to have to do that but I don't see any way around it. I've actually gone so far as to create a macro:

    #define WNDCOUNTER(ClassName) int Window<ClassName>::s_instancecount=0;

    that I put at the top of each cpp. I hate this as I'm sure you can imagine.
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  5. #5
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Well Fordy's solution does address what you are saying now whereas mine doesn't. Also you can put your macros into a header file.

    myheader.h
    Code:
    #ifndef MY_HEADER_FILE
    #define MY_HEADER_FILE
    
    
    #define WNDCOUNTER(ClassName) int Window<ClassName>::s_instancecount=0;
    
    #endif
    Then using #include "myheader.h" in each c file.

  6. #6
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    I just caught it.....

    template<class T>
    int MyTemplate<T>::counter;


    thanks fordy
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 48
    Last Post: 09-26-2008, 04:45 AM
  2. About aes
    By gumit in forum C Programming
    Replies: 13
    Last Post: 10-24-2006, 04:42 PM
  3. error: template with C linkage
    By michaels-r in forum C++ Programming
    Replies: 3
    Last Post: 05-17-2006, 09:11 AM
  4. Half-life SDK, where are the constants?
    By bennyandthejets in forum Game Programming
    Replies: 29
    Last Post: 08-25-2003, 12:58 PM
  5. structure vs class
    By sana in forum C++ Programming
    Replies: 13
    Last Post: 12-02-2002, 07:18 AM

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