Jump to content
  • Advertisement

Archived

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

gimp

Passing Templates as Params

This topic is 6156 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

My XML parser uses a factory to create objects from the elements in the .xml file. For different files however I''d want to use a different factory (ie the video config vs the wireframe renderstate). So, I''d like my XML parser to allowa factory to be passed in. Normally this would be as simple as passing in a ref\pointer to a factory, except that my factory is actually a template. How do denote the ability for the following to both to be passed in as a param? CFactory CFactory Both support the factory methods the parser expects to call... Chris Brodie http:\\fourth.flipcode.com

Share this post


Link to post
Share on other sites
Advertisement
I'm assuming that you lost your < and > and everything in between. =) For future reference, to make your < and > appear properly in any HTML page (as are these messages), use the character codes &lt; and &gt; respectively.

If you're asking how to pass a reference to a specialized template (a template class instantiated for a specific type), then I think this is how it's done:

      
template<class T>
class blah
{
...
};

void func(blah<int> param)
{
...
}

// here we're overloading func to handle chars, too

void func(blah<char> param)
{
...
}

// but we don't have to overload if we don't want to

void func2(blah<float> param)
{
...
}

void main()
{
blah<int> a;
blah<char> b;
blah<double> c;

func(a);
func(b);
func(c); // ERROR: no overload for func that takes blah<float>...

func2(c); // ...but func2 can

}


However, if you're asking how to pass any object of a specific template type--which is what I think you're asking for--then I think this works:

  
template<class T>
class blah
{
...
};

// since func is a template function taking a parameter of blah specialized for type T, it can work with any specialization of blah

template<class T>
void func(blah<T> param)
{
...
}

void main()
{
blah<int> a;
blah<float> b;

func(a);
func(b);
}


I hope I answered your question somewhere in here.


Edited by - merlin9x9 on July 17, 2001 1:42:23 AM

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.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!