Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 153133
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T09:44:50+00:00 2026-05-11T09:44:50+00:00

I have a template class defined in a header file like this. Here I

  • 0

I have a template class defined in a header file like this. Here I have defined a static variable as well:

#ifndef TEST1_H_ #define TEST1_H_  void f1();  static int count;  template <class T> class MyClass { public:      void f()     {         ++count;     }   };  #endif 

And I have defined main() function in a different cpp file like this:

int main(int argc, char* argv[]) {     MyClass<int> a;     a.f();     f1();      cout<<'Main:' << count << '\n';      return 0; } 

I have implemented function f1() in a different cpp file like this:

void f1() {     MyClass<int> a;     a.f();      cout<<'F1: ' <<count <<'\n'; } 

When I compiled this using VC6, I got the output as ‘F1:0 Main:2’. How is this possible? Also, in general how should I handle if I want to use static variables along with templates?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T09:44:50+00:00Added an answer on May 11, 2026 at 9:44 am

    You’re getting two copies of the same variable because you’ve declared a static variable in a header file. When you declare a global variable static this way, you’re saying it’s local to the compilation unit (the .o file). Since you include the header in two compilation units, you get two copies of count.

    I think what you really want here is a static template member variable associated with each instance of the template class. It would look like this:

    template <class T> class MyClass {     // static member declaration     static int count;     ... };  // static member definition template<class T> int MyClass<T>::count = 0; 

    This will get you a count for each instantiation of your template. That is, you’ll have a count for MyClass<int>, MyClass<foo>, MyClass<bar>, etc. f1() would now look like this:

    void f1() {     MyClass<int> a;     a.f();      cout<<'F1: ' << MyClass<int>::count <<'\n'; } 

    If you want a count for all instantiations of MyClass (regardless of their template parameters), you do need to use a global variable.

    However, you probably don’t want a global variable directly because you run the risk of using it before it gets initialized. You can get around this by making a global static method that returns a reference to your count:

    int& my_count() {     static int count = 0;     return count; } 

    Then accessing it from within your class like this:

    void f() {     ++my_count(); } 

    This will ensure that count gets initialized before it’s used, regardless of which compilation unit you access it from. See the C++ FAQ on static initialization order for more details.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a C header file like this: #ifndef RENDERER_H #define RENDERER_H static int
Basically I have code which looks like this inside a header file: class Bar;
I have a template defined in my header file as follows: template<typename T> class
I have a template class defined like so: template <class T> class Command {
In one header file, i have a inline function define like bellow: extern CMODEXPORT
I have a template matrix class class defined in a header called Matrix.h. Certain
I can't understand, why if we define static variable of usual (non-template) class in
I have two classes as follows in a header file template<size_t N> class Parent{
I have a templated class defined (in part) as template <class T> MyClass {
I have a template class like below. template<int S> class A { private: char

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.