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

📄 [20] inheritance virtual functions, c++ faq lite.htm

📁 c++faq。里面有很多关于c++的问题的解答。
💻 HTM
📖 第 1 页 / 共 2 页
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3c.org/TR/1999/REC-html401-19991224/loose.dtd">
<!-- saved from url=(0055)http://www.sunistudio.com/cppfaq/virtual-functions.html -->
<HTML><HEAD><TITLE>[20] Inheritance virtual functions, C++ FAQ Lite</TITLE>
<META http-equiv=Content-Type content="text/html; charset=gb2312">
<META http-equiv=Content-Language content=zh-cn>
<META content=virtual-functions.html name=FILENAME>
<META content="[20] Inheritance virtual functions, C++ FAQ Lite" name=ABSTRACT>
<META content=cline@parashift.com name=OWNER>
<META content="Marshall Cline, cline@parashift.com" name=AUTHOR>
<META content="MSHTML 6.00.2462.0" name=GENERATOR>
<META content=FrontPage.Editor.Document name=ProgId><LINK rev=made 
href="mailto:cline@parashift.com"><LINK 
href="[20] Inheritance virtual functions, C++ FAQ Lite.files/cpp-faq.css" 
type=text/css rel=stylesheet></HEAD>
<BODY>
<H1><A name=top></A>[20] 继承 — 虚函数<BR><SMALL><SMALL>(Part of <A 
href="http://www.sunistudio.com/cppfaq/index.html"><EM>C++ FAQ Lite</EM></A>, <A 
href="http://www.sunistudio.com/cppfaq/copy-permissions.html#[1.2]">Copyright&nbsp;&copy; 
1991-2001</A>, <A href="http://www.parashift.com/" target=OutsideTheFAQ>Marshall 
Cline</A>, <A 
href="mailto:cline@parashift.com">cline@parashift.com</A>)</SMALL></SMALL></H1>
<P>简体中文版翻译:<A href="http://www.sunistudio.com/nicrosoft">申旻</A>,<A 
href="mailto:nicrosoft@sunistudio.com">nicrosoft@sunistudio.com</A>(<A 
href="http://www.sunistudio.com/">东日制作室</A>,<A 
href="http://www.sunistudio.com/asp/sunidoc.asp">东日文档</A>)</P>
<HR>

<H3>FAQs in section [20]:</H3>
<UL>
  <LI><A 
  href="http://www.sunistudio.com/cppfaq/virtual-functions.html#[20.1]">[20.1] 
  什么是“虚成员函数”?</A> 
  <LI><A 
  href="http://www.sunistudio.com/cppfaq/virtual-functions.html#[20.2]">[20.2] 
  C++ 怎样同时实现动态绑定和静态类型?</A> 
  <LI><A 
  href="http://www.sunistudio.com/cppfaq/virtual-functions.html#[20.3]">[20.3] 
  虚成员函数和非虚成员函数调用方式有什么不同?</A> 
  <LI><A 
  href="http://www.sunistudio.com/cppfaq/virtual-functions.html#[20.4]">[20.4] 
  析构函数何时该时虚拟的?</A> 
  <LI><A 
  href="http://www.sunistudio.com/cppfaq/virtual-functions.html#[20.5]">[20.5] 
  什么是“虚构造函数(<TT>virtual</TT> constructor)”?</A> </LI></UL>
<P>
<HR>

<P><A name=[20.1]></A>
<DIV class=FaqTitle>
<H3>[20.1] 什么是“虚成员函数”?</H3></DIV>
<P>从面向对象观点来看,它是 C++ 最重要的特征:<A 
href="http://www.sunistudio.com/cppfaq/big-picture.html#[6.8]">[6.8]</A>, <A 
href="http://www.sunistudio.com/cppfaq/big-picture.html#[6.9]">[6.9]</A>. 
<P>虚函数允许派生类取代基类所提供的实现。编译器确保当对象为派生类时,取代者(译注:即派生类的实现)总是被调用,即使对象是使用基类指针访问而不是派生类的指针。这样就允许基类的算法被派生类取代,即使用户不知道派生类的细节。 

<P>派生类可以完全地取代基类成员函数(覆盖(override)),也可以部分地取代基类成员函数(增大(augment))。如果愿意的话,后者由派生类成员函数调用基类成员函数来完成。 

<P><SMALL>[&nbsp;<A 
href="http://www.sunistudio.com/cppfaq/virtual-functions.html#top">Top</A> 
|&nbsp;<A 
href="http://www.sunistudio.com/cppfaq/virtual-functions.html#bottom">Bottom</A> 
|&nbsp;<A 
href="http://www.sunistudio.com/cppfaq/basics-of-inheritance.html">Previous&nbsp;section</A> 
|&nbsp;<A 
href="http://www.sunistudio.com/cppfaq/proper-inheritance.html">Next&nbsp;section</A> 
]</SMALL> 
<HR>

<P><A name=[20.2]></A>
<DIV class=FaqTitle>
<H3>[20.2] C++ 怎样同时实现动态绑定和静态类型?</H3></DIV>
<P>当你有一个对象的指针,而对象实际是该指针类型的派生类(例如:一个 Vehicle*指针实际指向一个Car 
对象)。由此有两种类型:指针的(静态)类型(在此是Verhicle),和指向的对象的(动态)类型(在此是Car)。
<P><I>静态类型</I>意味着成员函数调用的合法性被尽可能早地检查:编译器在编译时。编译器用指针的静态类型决定成员函数调用是否合法。如果指针类型能够处理成员函数,那么指针所指对象当然能很好的处理它。例如,如果&nbsp;Vehicle&nbsp;有某个成员函数,则由于Car是一种Vehicle,那么Car&nbsp;当然也有该成员函数。 

<P><I>动态绑定</I>意味着成员函数调用的代码地址在最终时刻才被决定:基于运行时的对象动态类型。因为绑定到实际被调用的代码这个过程是动态完成的(在运行时),所以被称为“动态绑定”。动态绑定是虚函数导致的结果之一。 

<P><SMALL>[&nbsp;<A 
href="http://www.sunistudio.com/cppfaq/virtual-functions.html#top">Top</A> 
|&nbsp;<A 
href="http://www.sunistudio.com/cppfaq/virtual-functions.html#bottom">Bottom</A> 
|&nbsp;<A 
href="http://www.sunistudio.com/cppfaq/basics-of-inheritance.html">Previous&nbsp;section</A> 
|&nbsp;<A 
href="http://www.sunistudio.com/cppfaq/proper-inheritance.html">Next&nbsp;section</A> 
]</SMALL> 
<HR>

<P><A name=[20.3]></A>
<DIV class=FaqTitle>
<H3>[20.3] 虚成员函数和非虚成员函数调用方式有什么不同?</H3></DIV>
<P>非虚成员函数是静态确定的。也就是说,该成员函数(在编译时)被静态地选择,该选择基于指象对象的指针(或引用)的类型。

⌨️ 快捷键说明

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