template with vc6

Started by
14 comments, last by supagu 20 years, 1 month ago
the following tempalte:

class RegBase
{
public:
	virtual Obj* Create()
	{
		return 0;
	}
};

template<class T> class ObjReg : public RegBase
{
public:
	ObjReg( const char* classId )
	{
		ObjMgr::Register( classId, (RegBase*)this );
	}

	Obj* Create()
	{
		printf("Creating the class complete@!!!\n");
		return new T;
		//return 0;

	}

protected:

};
stops me compiling in vc6 when i got use it like: ObjReg regReg("chat"); the error says: f:\siphongl\engine\objmgr.h(61) : error C2259: 'Chat' : cannot instantiate abstract class due to following members: f:\siphongl\engine\objmgr.h(59) : while compiling class-template member function 'class Obj *__thiscall ObjReg::Create(void)' clicking on the error points to the line "return new T;" it works compiles fine on linux, so im guessing its a vc6 issue, would be nice to find a fix for it tho, or some alternative :-/ the idea of the tempalte is to register a class, and provide a method to create an instance of the class for my network code. [edited by - supagu on February 28, 2004 6:44:20 AM]
Advertisement
I don't know what T is, and I'm no template professional, but I assume the correct syntax would be

template < typename T > class ...

Victor Nicollet, INT13 game programmer



[edited by - ToohrVyk on February 28, 2004 6:36:10 AM]
fixed the source brackets ;p
i did some more testing

//ALSO DOESNT WORKObj Create()	{		printf("Creating the class complete@!!!\n");		return T;		//return 0;	}//WORKS, but its not a pointer :-/Obj& Create()	{		printf("Creating the class complete@!!!\n");		return T;		//return 0;	}
Whenever you use a template, you must specify the data type for the template to use. Lets assum you want to store characters in your template class, you could do that with the code.

NOTE: / = greater than sign      \ = less than signObjReg /char\ regReg("chat");    


I would suggest rewritting your code tho. Right now the template class inherits from a non-template base class, which defeats the whole point of templates. You should rewrite your code so that the template classes only inherits from other template classes.



[edited by - Onemind on February 28, 2004 8:45:33 PM]
i had to inherit from a base so i could keep a list of them,

when the template is created, it is addeed to a list, so that when i call for example:

CreateClass("chat")
it will find the template instance which is responsible for creating the chat class, then call:
myTemplate->Create()
surely there has to be some way around this in vc6? a template to create an instance of T
:-/
Right now i''m running low on nicotine, bloodsugar and caffeine, so i might''ve totally missed the point here.
But, create a standard interface (Say you call it IObjInterface) that you let all the classes inherit. Then make a list of the type IObjInterface, and every time you want to add an object, downcast it to the interface. No need for templates for that, and insisting on using them when they''re not needed is bad coding practice


--
MFC is sorta like the swedish police... It''''s full of crap, and nothing can communicate with anything else.
the point of the template, is so that it can register the class T, and create an instance of it, so i think i need temapltes :-/

unless i do something like:

class Register{Register( std::string sClass ){  ObjMgr::Register(this, sClass );}  virtual Obj* Create() = 0;};class ChatRegister : public Register{  ChatRegister(){  Register::Register("chat");}  Obj* Create(){  return new Chat();}  static ChatRegister; //created at load time to register self}


but then i have to make a class like ChatRegister for each class i want registered :-/

maybe template all of it except the create functions :-/
Like i said, create a common interface, let all the objects you'd want to register inherit this interface, and make the list out of the interface-class instead.

class IObjInterface{ int m_A;};class CObject : public IObjInterface{ float m_B;};class CObjReg{ Register(IObjInterface* p_Obj);}; CObject MyObject; CObjReg MyReg; MyReg.Register((IObjInterface*)&MyObject);  


Something along those lines.


--
MFC is sorta like the swedish police... It''s full of crap, and nothing can communicate with anything else.

[edited by - tok_junior on February 29, 2004 6:35:42 AM]

This topic is closed to new replies.

Advertisement