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

Ask A Question

Stats

  • Questions 177k
  • Answers 177k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer A quick check is to see if you can talk… May 12, 2026 at 3:25 pm
  • Editorial Team
    Editorial Team added an answer I use that approach too and can't praise it highly… May 12, 2026 at 3:25 pm
  • Editorial Team
    Editorial Team added an answer Did you point to the eclipse home correctly? all it… May 12, 2026 at 3:25 pm

Related Questions

I have a template matrix class class defined in a header called Matrix.h. Certain
I currently have a program where my main code is in a file main.cpp.
noob here still experimenting with templates. Trying to write a message processing class template
I'm having trouble deciding whether not this code should compile or if just both

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.