Skip to main content
gamedev.net
🔒 Locked

Static Methods in templates?

Started by Samurai Jack Sep 22, 2005 at 5:05 AM 3 replies 350+ views
Original Post
Samurai Jack
Samurai Jack
Greetings! I was wondering if it is possible to use static methods in templates? I would like to have a template with static method Find that returns the derived or specialized type. Here is the idea: template class myResource { public: virtual void DoSomething() { cout << "Template class" << endl; } static myResource* Find(LPSTR FileName) { ..now the method would look up for the object in a list and return it. so far it works ok. } } //now the specialization template<> class myResource { public: void DoSomething() { cout << "Template derived" << endl; } } typedef myResource intres; my problem is, i can not call intres::Find("test.txt") because it will not find the static Find method. Any ideas?
Fruny
Fruny
When you define a class template specialization, you must provide ALL the members, not just those you wish to change. myResource doesn't have a static Find member function.
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
Samurai Jack
Samurai Jack
What's the point of using templates then?

I would just like to get derived list of ints, floats or my other struct. I do not want to rewrite the function FindByName for each of grups, because they shoud have everything in common but just the "T* m_pResource" line, which corresponds to int, float or whatever. But I do not want to write FindByName seperate for int, for float or for whatever I will add in future.

My basic concept isn't all right. The idea is to set myResource Name="Bud Spencer", value=5, Name = "Terence Hill", value = 5 in a list. Ok that works just fine. But I do not allow that someone comes and says "Bud Specner" value = 7, beacuse it allready exists. Instead, he could retrieve the pointer to the allready allocated object and then change it.

So, when I use myResource m_Person it is allready too late, beacuse m_Person allready exists when i try to use it. In case I would try myResource m_Person::Find("Bud Spencer") it would be ok even if "Bud Spencer" does not exist, the function would create a new instance or pass the existing. That's all there is.
_goat
_goat
Your misconception is that the specialised template is derived. It is not. It is just a specialisation of the myResource class that is used whenever anyone passes int as the type specifier. The only time you would use specialisation (or partial specialisation) would be when the type passed works differently to others, ie the behaviour is different. This is normally done for strings as opposed to numbers. It looks to me like you're going to have to redesign your approach to this problem.
MaulingMonkey
MaulingMonkey
Quote:
Original post by Samurai Jack
What's the point of using templates then?


For genericness of course.

The ability to completely redefine the class and it's members based on specialization can be useful.

Things arn't that hard if you use a templatized base:

template < typename T >struct example_base {    T common_function() {        ...    }};template < typename T >struct example : public example_base< T > {    T specializable_function() {        ...    }};template <>struct example< int > : public example_base< int > {    int specializable_function() {        ...specialization....    }};

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.