Jump to content
  • Advertisement
Sign in to follow this  
CoffeeMug

Class template vs. method template. Difference?

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

What's difference between this:
template<typename T>
class MyClass
{
public:
    void MyMethod(T t) { /* ... */ }
};
and this:
class MyClass
{
public:
    template<typename T>
    void MyMethod(T t) { /* ... */ }
};

Share this post


Link to post
Share on other sites
Advertisement
Here's how you'd use the first one:

MyClass< int > mc1;
mc1.MyMethod( 4 );
mc1.MyMethod( 20.0 ); //20.0 gets cast to an integer
mc1.MyMethod< double > ( 20.0 ); //ERROR: MyMethod is not a templatized thingamajig. T is defined at the variable's decleration.


Whereas here's how you'd use the second one:

MyClass mc2;
mc2.MyMethod< int >( 4 );
mc2.MyMethod< int >( 20.0 ); //20.0 gets cast to an integer
mc2.MyMethod( 20.0 ); //calls mc2.MyMethod< double >( 20.0 );

MyClass< int > mc3; //ERROR: MyClass is not a templatized thingamajig. T is defined only for MyMethod, and only as it's called.

Share this post


Link to post
Share on other sites
Ahh, so the first one creates multiple MyClass types while the second leaves me with one type but adds multiple methods to it. Gotcha, thanks!

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • 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!