So far I have:
// TypeCounter.h
template <typename Base>
class Counter : public Base
{
protected:
static int typeIndexCounter;
};
template <typename T, typename Base>
class Bridge : public Counter<Base>
{
public:
virtual ~Bridge() {}
virtual int GetTypeIndex() const
{
return TypeIndex();
}
static int TypeIndex()
{
static int typeIndex = typeIndexCounter++;
return typeIndex;
}
};
// static variable definition
template <typename Base>
int Counter<Base>::typeIndexCounter;
Use case is like:
class SBase
{ ... }
class S0 : public Bridge<S0, SBase>
{ ... }
class S1 : public Bridge<S1, SBase>
{ ... }
class S2 : public Bridge<S2, SBase>
{ ... }
So for each kind of Base class, I can count how many derives it has (if it has ever been instantiated, of course).
The problem is that, this Counter class is in a DLL.
If I have multiple users of this DLL, then each user would instantiate its own Bridge and Counter, then the TypeIndex() can be different among these users for the same T.
I tried to put __declspec(dllexport) before Counter and Bridge, but there are compile errors on the user side (the user side would change export to import, by defining a macro).
Despite the errors, it is also a wrong approach, since one should not export class template.
So I want to make sure that the TypeIndex() keep the same for certain T, even if there are many instantiations for the same T, how can I achieve this?
Thanks!