Jump to content

Templating members


Recommended Posts



I am currently working on a service provider which will hold instances of classes such as the renderer and the state manager which all derive from the Service class. To do so I though I could use templating.


Broken header file:


#include <map>

#include "Service.h"

class ServiceProvider

// Methods.

template<class Type>
void AddService(Type type, Service* service);

template <class Type>
Service* GetService(Type type);


// Globals.

map<class type, Service*> services;


I want to be able to store all these services in the map with the type as the key so I can do GetService<StateManager> for example. Is it possible to template member attributes this way?



Link to comment
Share on other sites

A hack around typeof() would be to make a pure virtual method in your base class called string GetType(). Then every derived class needs to override it and return it's type.


class Base



virtual string GetType()=0;



class Child1 : public Base



virtual string GetType() { return "CHILD1"; }


Link to comment
Share on other sites



I think you are more looking for a factroy pattern...


i would do something like this:


First define a virtual class called Service.


class CService
float m_fMyVar;
UINT8 m_uiMyType;
virtual ~CService();
UINT8 GetType(){return m_uiType;};
virtual void Setup() = 0;
virtual void Execute() = 0;
virtual void Shutdown() = 0;
typedef CService*    LPSERVICE;

This is your pure virutal BaseService class. Now you can use it as the base for all the service youll provide.

Just do the following.


 class CMyNewService : public CService
void Setup(){ m_fMyVar = 42; uiMyType = 1; };
void Execute(){ m_fMyVar++; };
void Shutdown(){ m_fMyVar = 0; };
typedef CMyNewSerrvice*    LPMYNEWSERVICE;


Then you have to create a factory or a managerclass.

class CServiceProvider{
map<UINT8, LPCSERVICE> m_mapService;
void Register( LPCSERVICE _pService ){ m_mapService[ _pService->GetType() ] = _pService; };
LPCSERVICE Get( UINT8 _uiService ){ return m_mapService[ _uiService ]; };


The usage looks like this


 LPMYNEWSERVICE m_pService = new CMyNewService();
CServiceProvider  cServiceProvider;
cServiceProvider.Register( m_pService );
LPSERVICE m_pCOMMON_SERVICE = cServiceProvider.Get( 1 );


Hopefully i understood you right!?


this is right from brain to forum so no testing ;)




Windows Vista 64 / Win7 64 | 12GB DDR3 1600 | I7 965 | 2 * 280GTX sli | 2 * 300GB Raptor | 2 * 1.5TB

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


  • Create New...