⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 subject_62847.htm

📁 vc
💻 HTM
字号:
<p>
序号:62847 发表者:amber 发表日期:2003-11-29 09:34:22
<br>主题:虚函数的问题
<br>内容:虚函数的标准调用是这样的:<BR>derived tmp;<BR>base * p = &amp;tmp;<BR>p-&gt;f1();//会调用derived::f1()函数<BR><BR>虚函数的标准实现是这样的<BR>class base<BR>{<BR>public:<BR>&nbsp;&nbsp;&nbsp;&nbsp;int a;<BR>&nbsp;&nbsp;&nbsp;&nbsp;virtual int f1();<BR>};<BR>int base::f1()<BR>{<BR>&nbsp;&nbsp;&nbsp;&nbsp;int i = 1;return i;<BR>}<BR>class derived : public base<BR>{<BR>public:<BR>&nbsp;&nbsp;&nbsp;&nbsp;int b;<BR>&nbsp;&nbsp;&nbsp;&nbsp;virtual int f1();<BR>};<BR>int derived::f1()<BR>{<BR>&nbsp;&nbsp;&nbsp;&nbsp;int i = 0;return i;<BR>}<BR><BR>但是这样也可以实现<BR>class base<BR>{<BR>public:<BR>&nbsp;&nbsp;&nbsp;&nbsp;int a;<BR>&nbsp;&nbsp;&nbsp;&nbsp;virtual int f1();<BR>};<BR>int base::f1()<BR>{<BR>&nbsp;&nbsp;&nbsp;&nbsp;int i = 1;return i;<BR>}<BR>class derived : public base<BR>{<BR>public:<BR>&nbsp;&nbsp;&nbsp;&nbsp;int b;<BR>&nbsp;&nbsp;&nbsp;&nbsp;int f1();//注意,此处没有virtual前缀<BR>};<BR>int derived::f1()<BR>{<BR>&nbsp;&nbsp;&nbsp;&nbsp;int i = 0;return i;<BR>}<BR><BR>可否有那位探讨一下其中的异同。
<br><a href="javascript:history.go(-1)">返回上页</a><br><a href=http://www.copathway.com/cndevforum/>访问论坛</a></p>
<hr size=1>
<blockquote><p>
回复者:太平冰水 回复日期:2003-11-29 10:19:29
<br>内容:虚函数是通过虚函数表的指针在运行时动态确定调用,一般函数是在编译时就确定了
<br>
<a href="javascript:history.go(-1)">返回上页</a><br><a href=http://www.copathway.com/cndevforum/>访问论坛</a></p></blockquote>
<hr size=1>
<blockquote><p>
<font color=red>答案被接受</font><br>回复者:dsadsdas 回复日期:2003-11-29 11:44:50
<br>内容:不用探讨了。<BR>当derived里面定义一个和base里面的virtual函数一样的函数的时候,自动为virtual的。也就是说,虽然你没写,但是还是virtual的。
<br>
<a href="javascript:history.go(-1)">返回上页</a><br><a href=http://www.copathway.com/cndevforum/>访问论坛</a></p></blockquote>
<hr size=1>
<blockquote><p>
回复者:amber 回复日期:2003-11-29 12:31:25
<br>内容:楼上的,水平不错。
<br>
<a href="javascript:history.go(-1)">返回上页</a><br><a href=http://www.copathway.com/cndevforum/>访问论坛</a></p></blockquote>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -