Template class parameter

G

George2

Hello everyone,


Sometimes a template class is defined to have only parameter and no
type (e.g. class something) information. Like the below sample. My
question is, why define template class in this way (e.g. I can
implement in an alternative way to put the parameter into
constructor)? If there is no type information (e.g. class something),
there will be no benefits and reasons to use the template class. Any
comments?

Code:
#include <vector>
#include <iostream>

using namespace std;

template <int size> class Foo {

private:
	int limit;

public:
	Foo()
	{
		limit = size;
	}

};


int main (int argc, char** argv)
{
	Foo <100> f1; // limit member of instance f1 is 100

	Foo <200> f2; // limit member of instance f2 is 200

	return 0;
}


thanks in advance,
George
 
W

Walter Roberson

George2 said:
Sometimes a template class is defined to have only parameter and no
type (e.g. class something) information. Like the below sample. My
question is, why define template class in this way

C doesn't have templates, so I recommend that you ask in comp.lang.c++
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,074
Latest member
StanleyFra

Latest Threads

Top