<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Freya.no &#187; class</title>
	<atom:link href="http://wp.freya.no/tag/class/feed/" rel="self" type="application/rss+xml" />
	<link>http://wp.freya.no</link>
	<description>Knowledge is power</description>
	<lastBuildDate>Mon, 06 Feb 2012 19:56:41 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Class templates in C++</title>
		<link>http://wp.freya.no/c/class-templates-in-c/</link>
		<comments>http://wp.freya.no/c/class-templates-in-c/#comments</comments>
		<pubDate>Tue, 02 Mar 2010 21:58:18 +0000</pubDate>
		<dc:creator>Tatyana</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[c++]]></category>
		<category><![CDATA[class]]></category>
		<category><![CDATA[game]]></category>
		<category><![CDATA[point]]></category>
		<category><![CDATA[point3d]]></category>
		<category><![CDATA[template]]></category>
		<category><![CDATA[vector]]></category>

		<guid isPermaLink="false">http://wp.freya.no/</guid>
		<description><![CDATA[Templates in C++ are the way functions and classes operate with generic types. This allows a function or class to work on many different data types without being rewritten for each one [wiki]. Note that there is a difference between class template and a template class: class template is a template used to generate template [...]
No related posts.]]></description>
			<content:encoded><![CDATA[<p>Templates in  C++ are the way functions and classes operate with generic types. This allows a function or class to work on many different data types without being rewritten for each one [<a href="http://en.wikipedia.org/wiki/Template_%28programming%29">wiki</a>].</p>
<p>Note that there is a difference between class template and a template class: class template is a template used to generate template classes. You cannot declare an object of a class template. Template class is an instance of a class template<a href="http://scv.bu.edu/computation/bluegene/IBMdocs/compiler/xlc-8.0/html/language/ref/class_templates.htm">.</a></p>
<p>Here we are going to look at 3 main clas templates that are very useful in game and 3D design: Point, Vector and Matrix class templates.</p>
<h3>Point and Point3D:</h3>
<p>Here is how a simple Point template will look like in C++. This template is constructed to support multiple dimensions like Point2D, 3D and so on:</p>
<pre class="brush: cpp; title: ; notranslate">
template &lt;class T, int n&gt; class Point {
protected:
	T mat[n];
	 void cp(const Point&lt;T,n&gt; &amp;p)
    {
		memcpy(mat, p.mat, n*sizeof(T) );
	 }

    void cp(const T p[n])
    {
		memcpy(mat, p, n*sizeof(T) );
	}

    void cp(const T &amp;val)
    {
		for (int i=0; i&lt;n; i++)
			mat[i] = val;
    }

public:
	// new constructor
	Point(const T pt[n])
	{
		cp(pt);
	}

	// copy constructor
	Point(const Point&lt;T,n&gt; &amp;cp)
	{
		this-&gt;cp(cp);
	}

	// default constructor
	Point()
	{
		for (int i=0; i&lt;n; i++)
			mat[i] = 0;
	}
	virtual ~Point(void) {};
};
</pre>
<p>Templates are of great utility to programmers in C++, especially when combined with multiple inheritance and operator overloading. Here vi can add multiple operator overloading functions to our Point class template, for example addition:</p>
<pre class="brush: cpp; title: ; notranslate">
        //Point + some value
	Point&lt;T,n&gt; &amp;operator+=(const T &amp;val)
    {
		for (int i=0; i&lt;n; i++)
			mat[i] += val;

		return (*this);
    }

    //sum of two Points
    Point&lt;T,n&gt; &amp;operator+=(Point&lt;T,n&gt; &amp;pt)
    {
		for (int i=0; i&lt;n; i++)
			mat[i] += pt[i];

		return (*this);
    }
</pre>
<p>A Point3D class template will look like this:</p>
<pre class="brush: cpp; title: ; notranslate">
template &lt;class T&gt; class Point3D : public Point&lt;T,3&gt; {
public:
	Point3D() : Point&lt;T,3&gt;() {};

	Point3D(const Point3D&lt;T&gt; &amp;p) : Point&lt;T,3&gt;(p) {
		for (int i=0; i&lt;3; i++)
		mat[i] = p.mat[i];
	}
	Point3D( const T px, const T py, const T pz)
	{
		mat[0]=px; mat[1]=py; mat[2]=pz;
	}
};
</pre>
<p>An example of how we can use it is a motion programming:</p>
<pre class="brush: cpp; title: ; notranslate">
	Point3D&lt;float&gt; EulerAngle(float sensitivity, double x, double y, double z) {
		return Point3D&lt;float&gt; (
				sensitivity*cos(z),
				sensitivity*sin(z),
				0
		);
	}
</pre>
 <img src="http://wp.freya.no/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?view=1&post_id=598" width="1" height="1" style="display: none;" /><p><a class="a2a_dd a2a_target addtoany_share_save" href="http://www.addtoany.com/share_save#url=http%3A%2F%2Fwp.freya.no%2Fc%2Fclass-templates-in-c%2F&amp;title=Class%20templates%20in%20C%2B%2B" id="wpa2a_6"><img src="http://wp.freya.no/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share"/></a></p><p>No related posts.</p>]]></content:encoded>
			<wfw:commentRss>http://wp.freya.no/c/class-templates-in-c/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

