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 7761467
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T14:08:35+00:00 2026-06-01T14:08:35+00:00

If you have a template class with a static variable, is there any way

  • 0

If you have a template class with a static variable, is there any way to get the variable to be the same across all types of the class, rather than for each one?

At the moment my code is like this:

 template <typename T> class templateClass{
 public:
     static int numberAlive;
     templateClass(){ this->numberAlive++; }
     ~templateClass(){ this->numberAlive--; }
};

template <typename T> int templateClass<T>::numberAlive = 0;

And main:

templateClass<int> t1;
templateClass<int> t2;
templateClass<bool> t3;

cout << "T1: " << t1.numberAlive << endl;
cout << "T2: " << t2.numberAlive << endl;
cout << "T3: " << t3.numberAlive << endl;

This outputs:

 T1: 2
 T2: 2
 T3: 1

Where as the desired behaviour is:

 T1: 3
 T2: 3
 T3: 3

I guess I could do it with some sort of global int that any type of this class increments and decrements, but that doesnt seem very logical, or Object-Oriented

Thank you anyone who can help me implement this.

  • 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. Editorial Team
    Editorial Team
    2026-06-01T14:08:37+00:00Added an answer on June 1, 2026 at 2:08 pm

    Have all the classes derive from a common base class, whose only responsibility is to contain the static member.

    class MyBaseClass {
    protected:
        static int numberAlive;
    };
    
    template <typename T>
    class TemplateClass : public MyBaseClass {
    public:
        TemplateClass(){ numberAlive++; }
       ~TemplateClass(){ numberAlive--; }
    };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need a way to have a single static variable for all kinds of
I can't understand, why if we define static variable of usual (non-template) class in
If I have: template <class T> class A { static void f() { //
I have the following template class and a (global) variable of its type: template
I have a template class defined like so: template <class T> class Command {
I have non-template class with a templatized constructor. This code compiles for me. But
I have the template class and array of pointers to objects and overloaded logic
I have a template class with a variadic template member function that I am
I want to have a template class that looks something like what I have
I have a class template <typename Iterator, typename Value> class Foo { public: Foo(const

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.