Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

Grizwald

Templated Factory Function

This topic is 5746 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I have a class, called CEngine. This class is NOT templated, but i have a method in it i want templated called AllocateEngineObject. here is the function prototype:
    
template <typename T> 
  T *AllocateEngineObject();
  
here is the implementation:
        
template <typename T>
  T *CEngine::AllocateEngineObject()
  {
	T *temp = new T(this);
	EngineObjects.push_back (temp);
 	EngineObjectCount++;
 	return T;
  }
  
  
now this compiles without error. But when the function is used in this way:
        
Driver = AllocateEngineObject<CWindowsDriver>(); 
  
EngineObjects is an STL vector of type IEngineObject. CWindowsDriver is derived from IEngineObject so it would be a legal downcast, right? Driver is a pointer to CWindowsDriver. i get these errors: error C2275: 'CWindowsDriver' : illegal use of this type as an expression error C2059: syntax error : ')' if you need me to clear anything up, please ask. Thanks for any help. -Cody [edited by - Grizwald on October 21, 2002 9:38:11 PM]

Share this post


Link to post
Share on other sites
Advertisement
There is an error in the return function. You return a type T. You should return temp.

template T *CEngine::AllocateEngineObject()
{
T *temp = new T(this);
EngineObjects.push_back (temp);
EngineObjectCount++;
return T; }

It is not possible to partialize template in function templates. The class must be templatized.

Kuphryn

[edited by - kuphryn on October 21, 2002 10:04:15 PM]

Share this post


Link to post
Share on other sites
thanks for the replies. So it is absolutely impossible to have a member-function template when the class isn''t templatized? that would suck if it is! If it is, how could i go about doing the same thing without templatizing the class...thanks again

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
http://www.dofactory.com/patterns/pattern_factory.asp

Share this post


Link to post
Share on other sites
quote:
Original post by Grizwald
thanks for the replies. So it is absolutely impossible to have a member-function template when the class isn''t templatized? that would suck if it is! If it is, how could i go about doing the same thing without templatizing the class...thanks again


You can do that, you just aren''t allowed to partially-specialize them - which you''re not. The only error that you are attemping to return T, which is a type (like int or float) not a pointer to one.

Share this post


Link to post
Share on other sites
i am returning temp instead of T now (stupid error ) but i am still getting the same errors! quiero matar mi computadora!

Share this post


Link to post
Share on other sites
The poor compiler thought you meant:

Driver = AllocateEngineObject [IsLessThan] CWindowsDriver [IsGreaterThan] ();

Because the template meaning of angle brackets are not part of the grammar for expressions unless you're using dynamic_cast or one of those types of keywords.

[edited by - Nypyren on October 22, 2002 10:24:59 PM]

Share this post


Link to post
Share on other sites
No, no, it will work that way. the use of the angle brackets is allowed in that situation.
And the solution has already been posted. Change the code you wrote for the factory function to return temp, not T.

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!