return static template class object

so i have a singleton class and this function is what i use to get the instance of my ResourceManager class.
1
2
3
4
5
static ResourceManager& getResourceManagerInstance()
{
   static ResourceManager<T> instance;
   return instance;
}


can someone explain to me what happens here if i do this:
1
2
ResourceManager<TypeA>::getResourceManagerInstance().someFunction();
ResourceManager<TypeB>::getResourceManagerInstance().someFunction();

the only think i can see here is that after these 2 calls i have now 2 static ResourceManager object with 2 difference template argument.

1
2
ResourceManager<TypeA>::getResourceManagerInstance();
ResourceManager<TypeB>::getResourceManagerInstance();

Now you have 2 objects: one type ResourceManager<TypeA> and one ResourceManager<TypeB>

1
2
ResourceManager<TypeA>::getResourceManagerInstance().someFunction();
ResourceManager<TypeB>::getResourceManagerInstance().someFunction();

now you have whatever someFunction(), called by each of the above object, returns; if it's void then you have nothing
@gunnerfunner

hello, thanks for that.


but i cant define it outside the class

it says:
invalid use of template-name 'ResourceManager' without an argument list

here is the code:
1
2
3
4
5
6
7
8
9
10
11
class ResourceManager 
{
      static ResourceManager& getResourceManagerInstance();
};

template<typename T> ResourceManager& ResourceManager<T>::getResourceManagerInstance()
        {
            static ResourceManager<T> instance;
            return instance;
        }
Yes you can:
1
2
3
4
5
6
7
8
9
10
11
12
template <typename T>
class ResourceManager
{
    static ResourceManager& getResourceManagerInstance();
};
template <typename T>
ResourceManager<T>& ResourceManager<T>::getResourceManagerInstance()
{

            static ResourceManager<T> instance;
            return instance;
}

or if you want to keep it within the class:
1
2
3
4
5
6
7
8
9
template <typename T>
class ResourceManager
{
      static ResourceManager& getResourceManagerInstance()
      {
            static ResourceManager<T> instance;
            return instance;
      }
};
@gunnerfunner thanks !
Topic archived. No new replies allowed.