<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	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/"
		>
<channel>
	<title>Comments on: Covariant, Templatized Virtual Copy Constructors</title>
	<atom:link href="http://nerdland.net/2009/06/covariant-templatized-virtual-copy-constructors/feed/" rel="self" type="application/rss+xml" />
	<link>http://nerdland.net/2009/06/covariant-templatized-virtual-copy-constructors/</link>
	<description>Computer Science, Programming, and All Points Beyond</description>
	<lastBuildDate>Thu, 15 Jul 2010 12:47:41 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
	<item>
		<title>By: Java Tutorial : The super and this Keywords &#8211; Rocking Team &#124; Java WebDev Insider</title>
		<link>http://nerdland.net/2009/06/covariant-templatized-virtual-copy-constructors/comment-page-1/#comment-4787</link>
		<dc:creator>Java Tutorial : The super and this Keywords &#8211; Rocking Team &#124; Java WebDev Insider</dc:creator>
		<pubDate>Fri, 19 Mar 2010 23:43:25 +0000</pubDate>
		<guid isPermaLink="false">http://nerdland.net/?p=212#comment-4787</guid>
		<description>[...] Covariant, Templatized Virtual Copy Constructors « Nerdland [...]</description>
		<content:encoded><![CDATA[<p>[...] Covariant, Templatized Virtual Copy Constructors « Nerdland [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Tyler McHenry</title>
		<link>http://nerdland.net/2009/06/covariant-templatized-virtual-copy-constructors/comment-page-1/#comment-96</link>
		<dc:creator>Tyler McHenry</dc:creator>
		<pubDate>Tue, 14 Jul 2009 04:43:51 +0000</pubDate>
		<guid isPermaLink="false">http://nerdland.net/?p=212#comment-96</guid>
		<description>Thanks for the comments, Matthias. What you posted is indeed a bit more elegant for the case of simple virtual copy construction, since you don&#039;t have to inherit &lt;code&gt;Base&lt;/code&gt; directly in addition to &lt;code&gt;Cloneable&lt;/code&gt;.

It doesn&#039;t, however, provide covariancy. In this context, &quot;covariant&quot; means that if have an object whose static (aka compile-time) type is &lt;code&gt;Derived&lt;/code&gt;, you should be able to call &lt;code&gt;clone()&lt;/code&gt; on it and get back a &lt;code&gt;Derived*&lt;/code&gt;, not a &lt;code&gt;Base*&lt;/code&gt;. 

Doing this is simple if you build each class by hand, but it gets trick with templates because the compiler does not know about the inheritance relationship between &lt;code&gt;Base&lt;/code&gt; and &lt;code&gt;Derived&lt;/code&gt; when it is instantiating &lt;code&gt;Cloneable&lt;Base, Derived&gt;&lt;/code&gt;, so it won&#039;t let you have &lt;code&gt;Cloneable::clone()&lt;/code&gt; return a &lt;code&gt;Derived*&lt;/code&gt;, which is what you would need to have covariancy, because it doesn&#039;t know that that is legal. 

Try changing the return type of &lt;code&gt;Cloneable::clone()&lt;/code&gt; to &lt;code&gt;Derived*&lt;/code&gt;, in your example and see how the compiler complains.</description>
		<content:encoded><![CDATA[<p>Thanks for the comments, Matthias. What you posted is indeed a bit more elegant for the case of simple virtual copy construction, since you don&#8217;t have to inherit <code>Base</code> directly in addition to <code>Cloneable</code>.</p>
<p>It doesn&#8217;t, however, provide covariancy. In this context, &#8220;covariant&#8221; means that if have an object whose static (aka compile-time) type is <code>Derived</code>, you should be able to call <code>clone()</code> on it and get back a <code>Derived*</code>, not a <code>Base*</code>. </p>
<p>Doing this is simple if you build each class by hand, but it gets trick with templates because the compiler does not know about the inheritance relationship between <code>Base</code> and <code>Derived</code> when it is instantiating <code>Cloneable&lt;Base, Derived&gt;</code>, so it won&#8217;t let you have <code>Cloneable::clone()</code> return a <code>Derived*</code>, which is what you would need to have covariancy, because it doesn&#8217;t know that that is legal. </p>
<p>Try changing the return type of <code>Cloneable::clone()</code> to <code>Derived*</code>, in your example and see how the compiler complains.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Matthias Vallentin</title>
		<link>http://nerdland.net/2009/06/covariant-templatized-virtual-copy-constructors/comment-page-1/#comment-95</link>
		<dc:creator>Matthias Vallentin</dc:creator>
		<pubDate>Tue, 14 Jul 2009 02:53:12 +0000</pubDate>
		<guid isPermaLink="false">http://nerdland.net/?p=212#comment-95</guid>
		<description>To complete my earlier post, here is a small compiling example:

&lt;pre lang=&quot;cpp&quot;&gt;
template &lt;typename Derived, typename Base&gt;
class cloneable : public Base
{
public:
    Base* clone() const 
    { 
        return new Derived(static_cast&lt;const Derived&amp;&gt;(*this)); 
    } 
};

class graphic
{
public:
    graphic(const graphic&amp; rhs)
    {
    }

    virtual graphic* clone() const = 0;
};

class rectangle : public cloneable&lt;rectangle, graphic&gt;
{
public:
    rectangle()
      : cloneable&lt;rectangle, graphic&gt;(*this)
    {
    }

    rectangle(const rectangle&amp; rhs)
      : cloneable&lt;rectangle, graphic&gt;(rhs)
    {
    }
};

class square : public cloneable&lt;square, rectangle&gt;
{
public:
    square()
      : cloneable&lt;square, rectangle&gt;(*this)
    {
    }

    square(const square&amp; rhs)
      : cloneable&lt;square, rectangle&gt;(rhs)
    {
    }
};


int main()
{
    graphic* r = new rectangle;
    graphic* rc = r-&gt;clone();

    graphic* s = new square;
    graphic* sc = s-&gt;clone();

    delete r;
    delete rc;
    delete s;
    delete sc;

    return 0;
}
&lt;/pre&gt;

Hope this helps,

    Matthias</description>
		<content:encoded><![CDATA[<p>To complete my earlier post, here is a small compiling example:</p>
<pre lang="cpp">
template &lt;typename Derived, typename Base&gt;
class cloneable : public Base
{
public:
    Base* clone() const
    {
        return new Derived(static_cast&lt;const Derived&amp;&gt;(*this));
    }
};

class graphic
{
public:
    graphic(const graphic&amp; rhs)
    {
    }

    virtual graphic* clone() const = 0;
};

class rectangle : public cloneable&lt;rectangle, graphic&gt;
{
public:
    rectangle()
      : cloneable&lt;rectangle, graphic&gt;(*this)
    {
    }

    rectangle(const rectangle&amp; rhs)
      : cloneable&lt;rectangle, graphic&gt;(rhs)
    {
    }
};

class square : public cloneable&lt;square, rectangle&gt;
{
public:
    square()
      : cloneable&lt;square, rectangle&gt;(*this)
    {
    }

    square(const square&amp; rhs)
      : cloneable&lt;square, rectangle&gt;(rhs)
    {
    }
};

int main()
{
    graphic* r = new rectangle;
    graphic* rc = r-&gt;clone();

    graphic* s = new square;
    graphic* sc = s-&gt;clone();

    delete r;
    delete rc;
    delete s;
    delete sc;

    return 0;
}
</pre>
<p>Hope this helps,</p>
<p>    Matthias</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Matthias Vallentin</title>
		<link>http://nerdland.net/2009/06/covariant-templatized-virtual-copy-constructors/comment-page-1/#comment-94</link>
		<dc:creator>Matthias Vallentin</dc:creator>
		<pubDate>Tue, 14 Jul 2009 01:23:30 +0000</pubDate>
		<guid isPermaLink="false">http://nerdland.net/?p=212#comment-94</guid>
		<description>Very nice summary of a recurring problem! 

But wouldn&#039;t it be possible to keep covariance by simply adding a second
template parameter with the base type to the cloneable class template?

Here is an example taken from [1]:

&lt;pre lang=&quot;cpp&quot;&gt;
template &lt;typename Derived, typename Base&gt;
class clonable : public Base
{
public:
    Base* clone() const 
    { 
        return new Derived(static_cast&lt;const Derived&amp;&gt;(*this)); 
    } 
}

class graphic
{
public:
    virtual graphic* clone() const = 0;
};

class rectangle : public cloneable&lt;rectangle, graphic&gt; 
{ 
public: 
    rectangle(const rectangle &amp;); 
    ... 
}; 
 
class ellipse : public cloneable&lt;ellipse, graphic&gt; 
{ 
public: 
    ellipse(const ellipse &amp;); 
    ... 
}; 
&lt;/pre&gt;

Another benefit is that it removes redundancy in the class inheritance
definition. That is,

&lt;pre lang=&quot;cpp&quot;&gt;
class Derived : public Base, public Cloneable&lt;Base, Derived&gt;
&lt;/pre&gt;

becomes 

&lt;pre lang=&quot;cpp&quot;&gt;
class Derived : public Base, public Cloneable&lt;Base, Derived
&lt;/pre&gt;
in your example.


[1] http://www.two-sdg.demon.co.uk/curbralan/papers/accu/CloneAlone.pdf</description>
		<content:encoded><![CDATA[<p>Very nice summary of a recurring problem! </p>
<p>But wouldn&#8217;t it be possible to keep covariance by simply adding a second<br />
template parameter with the base type to the cloneable class template?</p>
<p>Here is an example taken from [1]:</p>
<pre lang="cpp">
template &lt;typename Derived, typename Base&gt;
class clonable : public Base
{
public:
    Base* clone() const
    {
        return new Derived(static_cast&lt;const Derived&amp;&gt;(*this));
    }
}

class graphic
{
public:
    virtual graphic* clone() const = 0;
};

class rectangle : public cloneable&lt;rectangle, graphic&gt;
{
public:
    rectangle(const rectangle &amp;);
    ...
}; 

class ellipse : public cloneable&lt;ellipse, graphic&gt;
{
public:
    ellipse(const ellipse &amp;);
    ...
};
</pre>
<p>Another benefit is that it removes redundancy in the class inheritance<br />
definition. That is,</p>
<pre lang="cpp">
class Derived : public Base, public Cloneable&lt;Base, Derived&gt;
</pre>
<p>becomes </p>
<pre lang="cpp">
class Derived : public Base, public Cloneable&lt;Base, Derived
</pre>
<p>in your example.</p>
<p>[1] <a href="http://www.two-sdg.demon.co.uk/curbralan/papers/accu/CloneAlone.pdf" rel="nofollow">http://www.two-sdg.demon.co.uk/curbralan/papers/accu/CloneAlone.pdf</a></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: No Factories Necessary « Nerdland</title>
		<link>http://nerdland.net/2009/06/covariant-templatized-virtual-copy-constructors/comment-page-1/#comment-13</link>
		<dc:creator>No Factories Necessary « Nerdland</dc:creator>
		<pubDate>Fri, 12 Jun 2009 12:08:35 +0000</pubDate>
		<guid isPermaLink="false">http://nerdland.net/?p=212#comment-13</guid>
		<description>[...] thought of a quick addendum to Monday&#8217;s article on covariant, templatized copy constructors. The end of that article said  [T]here is a large caveat that you may have noticed. Due to the the [...]</description>
		<content:encoded><![CDATA[<p>[...] thought of a quick addendum to Monday&#8217;s article on covariant, templatized copy constructors. The end of that article said  [T]here is a large caveat that you may have noticed. Due to the the [...]</p>
]]></content:encoded>
	</item>
</channel>
</rss>

<!-- Performance optimized by W3 Total Cache. Learn more: http://www.w3-edge.com/wordpress-plugins/

Minified using disk
Page Caching using disk (enhanced)
Database Caching 4/15 queries in 0.036 seconds using disk

Served from: nerdland.net @ 2010-09-07 11:01:26 -->