[prog] C++ Templates

Christine Bussman olearyck at SLU.EDU
Thu Jun 26 08:40:06 EST 2003


Here's a little fragment that I just wrote that will compile
(note that it's horrible in the sense of no constructor,
etc,
and there may be other errors that will show up only
when using the class (remember template classes are
weird that way), but this might at least get you started. 
If
you want longer code that really works, I have some, just
let me know and I can send it to you off list (I have some
class projects saved on a mac server).  The problem with
it is that it is 5 or more files long, etc.  The namespace
can sometimes make a real difference with the most
recent versions of gcc.

Christine

#include<iostream>

using namespace std;

template <class thing>
class foo
{
public:
	foo add( foo);
private:
	foo a;
	foo b;
};

template<class thing>
foo<thing> foo<thing>::add(foo other)
{
	foo result;
	result.a=a+other.a;
	result.b=b+other.b;
	return result;
}


More information about the Programming mailing list