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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T12:32:30+00:00 2026-05-18T12:32:30+00:00

I have a base class called Component which has a number of classes derived

  • 0

I have a base class called Component which has a number of classes derived from it. I want each class to have an integer associated with it (doesn’t matter which component gets what value, as long as they start at 0 and are contiguous). I don’t know how to do this directly, so in the same file as Component I added the following:

template <typename T>
class ComponentIdentifier
{
public:
    static unsigned int cid;
};

static unsigned int CIDCounter = 0;
template <typename T> unsigned int ComponentIdentifier<T> = CIDCounter++;

template <typename T> unsigned int ComponentID()
{
    return ComponentIdentifier<T>::cid;
}

unsigned int ComponentCount(); // Defined in .cpp file, just returns CIDCounter

Now I tested the ComponentID() function and it appears to work fine. Each component class that I tried ComponentID on returned a different integer just as I expected. However, whenever I call ComponentCount I get 0.

e.g. if I have the following lines of code:

std::cout << ComponentID<AAA>() << std::endl;
std::cout << ComponentID<BBB>() << std::endl;
std::cout << ComponentID<CCC>() << std::endl;
std::cout << ComponentCount() << std::endl;

then my output is:

0
1
2
0

I suspect what’s happening is that CIDCounter is being set to 0 again after its used to set the cid’s of each component, but I don’t know for sure and that seems kind of weird. Is there a way to do what I want or am I crazy and this whole plan doomed to failure?

  • 1 1 Answer
  • 1 View
  • 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-05-18T12:32:31+00:00Added an answer on May 18, 2026 at 12:32 pm

    You’ve declared your identifier as static:

    static unsigned int CIDCounter = 0;
    

    which means each compilation unit will get its own copy of the variable. Instead you should declare it as extern in the header file:

    extern unsigned int CIDCounter;
    

    and initialise it in the implementation file

    unsigned int CIDCounter = 0;
    

    One should note that without appropriate locks this won’t be thread-safe.

    To clarify further:

    The static keyword in this context means that the variable or function is limited to the current compilation unit (usually a cpp file). I’m guessing you have, say, a main.cpp and an idcounter.cpp – so now we have two variables (one for each of the compilation units) main_CIDCounter and idcounter_CIDCounter (variable names are for explanation only!).

    When you execute your test code in main.cpp the template function sees main_CIDCounter because that’s the current compilation unit and you get the expected 1,2,3. However, when you call ComponentCount() code from idcounter.cpp is used, and that code sees idcounter_CIDCounter – which hasn’t been modified at all. If you had other compilation units you would see similar behaviour, where each cpp file would appear to be maintaining its own ID counter.

    The fix I’ve described is simply to only have one copy of CIDCounter in idcounter.cpp, and declare it as external in all the other compilation units.

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

Sidebar

Related Questions

I have a base class called BaseEvent . And, 3 classes derived from BaseEvent
I have a base class called BaseClass . Many classes derived from the BaseClass
I have a base class called SCO that a number of classes inherit from.
I have a base and derived class called Node which form elements of a
I have a base class with a property called Name, which has an XmlText
I have a base class called Items and 3 derived classes, and within the
I have a base class called SpriteSheet which has two children, RotatedSpriteSheet and FlippedSpriteSheet.
I have a base class called Geometry from which there exists a subclass Sphere
I have a base class Foo that has an Update() function, which I want
I have a base class called Mijloc_transport and 2 derived classes called Masina and

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.