<?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>pangwa&#039;s Blog &#187; C/C++</title>
	<atom:link href="http://blog.pangwa.com/category/c_cpp/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.pangwa.com</link>
	<description>Always be pangwa</description>
	<lastBuildDate>Sat, 06 Aug 2011 15:21:04 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3</generator>
	<atom:link rel='hub' href='http://blog.pangwa.com/?pushpress=hub'/>
		<item>
		<title>Covariant return types</title>
		<link>http://blog.pangwa.com/2007/06/19/covariant-return-types/</link>
		<comments>http://blog.pangwa.com/2007/06/19/covariant-return-types/#comments</comments>
		<pubDate>Tue, 19 Jun 2007 13:18:55 +0000</pubDate>
		<dc:creator>pangwa</dc:creator>
				<category><![CDATA[C/C++]]></category>
		<category><![CDATA[C++]]></category>
		<category><![CDATA[Tech]]></category>

		<guid isPermaLink="false">http://blog.pangwa.com/?p=8</guid>
		<description><![CDATA[Covariant return types&#8211;翻译成中文的字面意思应该是协变返回类型, 它的意思可以概括为在面向对象的编程中, 子类在重写父类的方法时可以改变这个方法的返回值的类型. 一般来讲子类在重写(override)父类方法时, 要求这个方法在参数列表和返回值上都要和父类中的对应的方法保持相同的类型. 所以下面的代码是无法通过编译的: 也许大家还没有看到这个限制的不爽之处, 那么设想如下的情形, 你想为你的父类和子类都写一个clone ()方法, 返回一个clone出的对象的指针, 如果没有Covariant return type, 你需要写类似如下代码: 这里每个子类的clone方法都返回一个指向Base的指针, 如果想要转换成子类的指针还需要一次额外的类型转换, (当然也许你认为使用static_cast会在编译期间完成, 不会影响运行效率, 但显然代码不够优雅:) 如果使用Covariant return type, 我们就可以把每个子类的clone方法返回一个指向子类对象的指针类型了: 实际上Covariant return type就是用来这种在子类重写父类方法, 并且它的返回值的类型和父类方法的返回类型具有一定的相关性(一般是其衍生类型即派生类). 在C++标准03版的10.3.5中对其covaraiant return type做了详细的描述. 大家有兴趣可以去翻一下. Bookmark to:]]></description>
			<content:encoded><![CDATA[<p>Covariant return types&#8211;翻译成中文的字面意思应该是协变返回类型, 它的意思可以概括为在面向对象的编程中, 子类在重写父类的方法时可以改变这个方法的返回值的类型. 一般来讲子类在重写(override)父类方法时, 要求这个方法在参数列表和返回值上都要和父类中的对应的方法保持相同的类型.  所以下面的代码是无法通过编译的:</p>
<pre class="brush: cpp; title: ; notranslate">
class Base
{
public:
virtual int foo () {return 0;}
};

class Child : public Base
{
virtual long foo () {return 1;} //compile error, 'Child::foo': overridin
//virtual function return type differs
//and is not covariant from 'Base::foo'
};
</pre>
<p>也许大家还没有看到这个限制的不爽之处, 那么设想如下的情形, 你想为你的父类和子类都写一个clone ()方法, 返回一个clone出的对象的指针, 如果没有Covariant return type, 你需要写类似如下代码:</p>
<pre class="brush: cpp; title: ; notranslate">
class Base
{
public:
Base* clone ();
};
class Child : public Base
{
public:
Base* clone ();
};

Child* bp = static_cast&lt;child*&gt;(child.clone ());&lt;/child*&gt;
</pre>
<p>这里每个子类的clone方法都返回一个指向Base的指针, 如果想要转换成子类的指针还需要一次额外的类型转换, (当然也许你认为使用static_cast会在编译期间完成, 不会影响运行效率, 但显然代码不够优雅:) 如果使用Covariant return type, 我们就可以把每个子类的clone方法返回一个指向子类对象的指针类型了:</p>
<pre class="brush: cpp; title: ; notranslate">
class Base
{
public:
Base* clone ();
};
class Child : public Base
{
public:
Child* clone ();
};
Child* bp = child.clone (); //no extra type cast needed.
</pre>
<p>实际上Covariant return type就是用来这种在子类重写父类方法, 并且它的返回值的类型和父类方法的返回类型具有一定的相关性(一般是其衍生类型即派生类).  在C++标准03版的10.3.5中对其covaraiant return type做了详细的描述.  大家有兴趣可以去翻一下.</p>
<!-- Social Bookmarks BEGIN -->
<div class="social_bookmark">
<a><strong><em>Bookmark to:</em></strong></a>
<br />
<div class="d">
<br />
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://del.icio.us/post?url=http%3A%2F%2Fblog.pangwa.com%2F2007%2F06%2F19%2Fcovariant-return-types%2F&amp;title=Covariant+return+types" rel="nofollow" title="Add to&nbsp;Del.icio.us"><img class="social_img" src="http://blog.pangwa.com/wp-content/plugins/social-bookmarks/images/delicious.png" title="Add to&nbsp;Del.icio.us" alt="Add to&nbsp;Del.icio.us" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://digg.com/submit?phase=2&amp;url=http%3A%2F%2Fblog.pangwa.com%2F2007%2F06%2F19%2Fcovariant-return-types%2F&amp;title=Covariant+return+types" rel="nofollow" title="Add to&nbsp;digg"><img class="social_img" src="http://blog.pangwa.com/wp-content/plugins/social-bookmarks/images/digg.png" title="Add to&nbsp;digg" alt="Add to&nbsp;digg" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.facebook.com/sharer.php?u=http%3A%2F%2Fblog.pangwa.com%2F2007%2F06%2F19%2Fcovariant-return-types%2F" rel="nofollow" title="Add to&nbsp;Facebook"><img class="social_img" src="http://blog.pangwa.com/wp-content/plugins/social-bookmarks/images/facebook.png" title="Add to&nbsp;Facebook" alt="Add to&nbsp;Facebook" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.google.com/bookmarks/mark?op=edit&amp;output=popup&amp;bkmk=http%3A%2F%2Fblog.pangwa.com%2F2007%2F06%2F19%2Fcovariant-return-types%2F&amp;title=Covariant+return+types" rel="nofollow" title="Add to&nbsp;Google Bookmarks"><img class="social_img" src="http://blog.pangwa.com/wp-content/plugins/social-bookmarks/images/google.png" title="Add to&nbsp;Google Bookmarks" alt="Add to&nbsp;Google Bookmarks" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://reddit.com/submit?url=http%3A%2F%2Fblog.pangwa.com%2F2007%2F06%2F19%2Fcovariant-return-types%2F&amp;title=Covariant+return+types" rel="nofollow" title="Add to&nbsp;reddit"><img class="social_img" src="http://blog.pangwa.com/wp-content/plugins/social-bookmarks/images/reddit.png" title="Add to&nbsp;reddit" alt="Add to&nbsp;reddit" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.stumbleupon.com/submit?url=http%3A%2F%2Fblog.pangwa.com%2F2007%2F06%2F19%2Fcovariant-return-types%2F&amp;title=Covariant+return+types" rel="nofollow" title="Add to&nbsp;Stumble Upon"><img class="social_img" src="http://blog.pangwa.com/wp-content/plugins/social-bookmarks/images/stumbleupon.png" title="Add to&nbsp;Stumble Upon" alt="Add to&nbsp;Stumble Upon" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://myweb2.search.yahoo.com/myresults/bookmarklet?u=http%3A%2F%2Fblog.pangwa.com%2F2007%2F06%2F19%2Fcovariant-return-types%2F&amp;t=Covariant+return+types" rel="nofollow" title="Add to&nbsp;Yahoo My Web"><img class="social_img" src="http://blog.pangwa.com/wp-content/plugins/social-bookmarks/images/yahoo.png" title="Add to&nbsp;Yahoo My Web" alt="Add to&nbsp;Yahoo My Web" /></a>
<br />
</div>
</div>
<!-- Social Bookmarks END -->
]]></content:encoded>
			<wfw:commentRss>http://blog.pangwa.com/2007/06/19/covariant-return-types/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Post with codes.</title>
		<link>http://blog.pangwa.com/2007/06/12/post-with-codes/</link>
		<comments>http://blog.pangwa.com/2007/06/12/post-with-codes/#comments</comments>
		<pubDate>Tue, 12 Jun 2007 13:40:36 +0000</pubDate>
		<dc:creator>pangwa</dc:creator>
				<category><![CDATA[C/C++]]></category>
		<category><![CDATA[timestamp]]></category>

		<guid isPermaLink="false">http://pangwa.com/blog/?p=4</guid>
		<description><![CDATA[Bookmark to:]]></description>
			<content:encoded><![CDATA[<pre class="brush: cpp; title: ; notranslate">
#include &lt;stdio.h&gt;
int main ()
{
printf (&quot;hello world!&quot;);
return 0;
} &lt;/stdio.h&gt;</pre>
<!-- Social Bookmarks BEGIN -->
<div class="social_bookmark">
<a><strong><em>Bookmark to:</em></strong></a>
<br />
<div class="d">
<br />
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://del.icio.us/post?url=http%3A%2F%2Fblog.pangwa.com%2F2007%2F06%2F12%2Fpost-with-codes%2F&amp;title=Post+with+codes." rel="nofollow" title="Add to&nbsp;Del.icio.us"><img class="social_img" src="http://blog.pangwa.com/wp-content/plugins/social-bookmarks/images/delicious.png" title="Add to&nbsp;Del.icio.us" alt="Add to&nbsp;Del.icio.us" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://digg.com/submit?phase=2&amp;url=http%3A%2F%2Fblog.pangwa.com%2F2007%2F06%2F12%2Fpost-with-codes%2F&amp;title=Post+with+codes." rel="nofollow" title="Add to&nbsp;digg"><img class="social_img" src="http://blog.pangwa.com/wp-content/plugins/social-bookmarks/images/digg.png" title="Add to&nbsp;digg" alt="Add to&nbsp;digg" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.facebook.com/sharer.php?u=http%3A%2F%2Fblog.pangwa.com%2F2007%2F06%2F12%2Fpost-with-codes%2F" rel="nofollow" title="Add to&nbsp;Facebook"><img class="social_img" src="http://blog.pangwa.com/wp-content/plugins/social-bookmarks/images/facebook.png" title="Add to&nbsp;Facebook" alt="Add to&nbsp;Facebook" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.google.com/bookmarks/mark?op=edit&amp;output=popup&amp;bkmk=http%3A%2F%2Fblog.pangwa.com%2F2007%2F06%2F12%2Fpost-with-codes%2F&amp;title=Post+with+codes." rel="nofollow" title="Add to&nbsp;Google Bookmarks"><img class="social_img" src="http://blog.pangwa.com/wp-content/plugins/social-bookmarks/images/google.png" title="Add to&nbsp;Google Bookmarks" alt="Add to&nbsp;Google Bookmarks" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://reddit.com/submit?url=http%3A%2F%2Fblog.pangwa.com%2F2007%2F06%2F12%2Fpost-with-codes%2F&amp;title=Post+with+codes." rel="nofollow" title="Add to&nbsp;reddit"><img class="social_img" src="http://blog.pangwa.com/wp-content/plugins/social-bookmarks/images/reddit.png" title="Add to&nbsp;reddit" alt="Add to&nbsp;reddit" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.stumbleupon.com/submit?url=http%3A%2F%2Fblog.pangwa.com%2F2007%2F06%2F12%2Fpost-with-codes%2F&amp;title=Post+with+codes." rel="nofollow" title="Add to&nbsp;Stumble Upon"><img class="social_img" src="http://blog.pangwa.com/wp-content/plugins/social-bookmarks/images/stumbleupon.png" title="Add to&nbsp;Stumble Upon" alt="Add to&nbsp;Stumble Upon" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://myweb2.search.yahoo.com/myresults/bookmarklet?u=http%3A%2F%2Fblog.pangwa.com%2F2007%2F06%2F12%2Fpost-with-codes%2F&amp;t=Post+with+codes." rel="nofollow" title="Add to&nbsp;Yahoo My Web"><img class="social_img" src="http://blog.pangwa.com/wp-content/plugins/social-bookmarks/images/yahoo.png" title="Add to&nbsp;Yahoo My Web" alt="Add to&nbsp;Yahoo My Web" /></a>
<br />
</div>
</div>
<!-- Social Bookmarks END -->
]]></content:encoded>
			<wfw:commentRss>http://blog.pangwa.com/2007/06/12/post-with-codes/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

