如果一个函数被定义为虚函数,它究竟意味着什么?它与纯虚函数是相同的吗?
当前回答
Simula、c++和c#在默认情况下使用静态方法绑定,程序员可以通过将特定方法标记为虚拟来指定它们应该使用动态绑定。 动态方法绑定是面向对象编程的核心。
面向对象编程需要三个基本概念:封装、继承和动态方法绑定。
的实现细节 抽象要隐藏在背后 简单的接口。 继承允许将新的抽象定义为 某些的扩展或改进 现有的抽象,获得一些 或者说它的所有特征 自动。 动态方法绑定允许新抽象显示其新抽象 行为,即使在上下文中使用 这需要旧的抽象。
其他回答
虚函数是在基类中声明并由派生类重新定义的成员函数。虚函数是按继承顺序分层的。 当派生类不重写虚函数时,将使用在其基类中定义的函数。
纯虚函数是指相对于基类不包含任何定义的虚函数。 它在基类中没有实现。任何派生类都必须重写此函数。
Virtual functions must have a definition in base class and also in derived class but not necessary, for example ToString() or toString() function is a Virtual so you can provide your own implementation by overriding it in user-defined class(es). Virtual functions are declared and defined in normal class. Pure virtual function must be declared ending with "= 0" and it can only be declared in abstract class. An abstract class having a pure virtual function(s) cannot have a definition(s) of that pure virtual functions, so it implies that implementation must be provided in class(es) that derived from that abstract class.
I'd like to comment on Wikipedia's definition of virtual, as repeated by several here. [At the time this answer was written,] Wikipedia defined a virtual method as one that can be overridden in subclasses. [Fortunately, Wikipedia has been edited since, and it now explains this correctly.] That is incorrect: any method, not just virtual ones, can be overridden in subclasses. What virtual does is to give you polymorphism, that is, the ability to select at run-time the most-derived override of a method.
考虑下面的代码:
#include <iostream>
using namespace std;
class Base {
public:
void NonVirtual() {
cout << "Base NonVirtual called.\n";
}
virtual void Virtual() {
cout << "Base Virtual called.\n";
}
};
class Derived : public Base {
public:
void NonVirtual() {
cout << "Derived NonVirtual called.\n";
}
void Virtual() {
cout << "Derived Virtual called.\n";
}
};
int main() {
Base* bBase = new Base();
Base* bDerived = new Derived();
bBase->NonVirtual();
bBase->Virtual();
bDerived->NonVirtual();
bDerived->Virtual();
}
这个程序的输出是什么?
Base NonVirtual called.
Base Virtual called.
Base NonVirtual called.
Derived Virtual called.
Derived重写Base的每个方法:不仅是虚方法,而且是非虚方法。
我们看到,当你有一个基类指向派生类的指针(bDerived)时,调用NonVirtual调用基类实现。这是在编译时解析的:编译器看到bDerived是一个Base*,而NonVirtual不是virtual,所以它在类Base上进行解析。
然而,调用Virtual调用派生类实现。由于关键字virtual,方法的选择发生在运行时,而不是编译时。这里在编译时发生的事情是,编译器看到这是一个Base*,并且它正在调用一个虚方法,所以它插入一个对虚表的调用,而不是类Base。此虚表在运行时实例化,因此运行时解析为最派生的覆盖。
我希望这不会让你太困惑。简而言之,任何方法都可以被覆盖,但只有虚方法可以提供多态性,即运行时选择派生最多的覆盖。然而,在实践中,覆盖非虚拟方法被认为是不好的实践,很少被使用,所以很多人(包括撰写维基百科文章的人)认为只有虚拟方法可以被覆盖。
virtual关键字赋予c++支持多态的能力。当你有一个指向某个类的对象的指针时,例如:
class Animal
{
public:
virtual int GetNumberOfLegs() = 0;
};
class Duck : public Animal
{
public:
int GetNumberOfLegs() { return 2; }
};
class Horse : public Animal
{
public:
int GetNumberOfLegs() { return 4; }
};
void SomeFunction(Animal * pAnimal)
{
cout << pAnimal->GetNumberOfLegs();
}
在这个(愚蠢的)示例中,GetNumberOfLegs()函数根据所调用对象的类返回适当的数字。
现在,考虑函数“SomeFunction”。它不关心传递给它的是什么类型的animal对象,只要它是从animal派生的。编译器会自动地将任何Animal派生类强制转换为Animal,因为它是一个基类。
如果我们这样做:
Duck d;
SomeFunction(&d);
它会输出'2'。如果我们这样做:
Horse h;
SomeFunction(&h);
它会输出'4'。我们不能这样做:
Animal a;
SomeFunction(&a);
因为它不会编译,因为GetNumberOfLegs()虚函数是纯的,这意味着它必须通过派生类(子类)来实现。
纯虚函数主要用于定义:
A)抽象类
这些是基类,你必须从它们中派生出来,然后实现纯虚函数。
b)接口
这些是“空”类,其中所有的函数都是纯虚的,因此你必须派生然后实现所有的函数。
Simula、c++和c#在默认情况下使用静态方法绑定,程序员可以通过将特定方法标记为虚拟来指定它们应该使用动态绑定。 动态方法绑定是面向对象编程的核心。
面向对象编程需要三个基本概念:封装、继承和动态方法绑定。
的实现细节 抽象要隐藏在背后 简单的接口。 继承允许将新的抽象定义为 某些的扩展或改进 现有的抽象,获得一些 或者说它的所有特征 自动。 动态方法绑定允许新抽象显示其新抽象 行为,即使在上下文中使用 这需要旧的抽象。
推荐文章
- c++中有最大数组长度限制吗?
- 什么是“参数依赖查找”(又名ADL,或“Koenig查找”)?
- 公共朋友交换成员函数
- 如何在Go中使用c++
- 自定义c++分配器的引人注目的例子?
- RAII和c++中的智能指针
- 如何构建和使用谷歌TensorFlow c++ api
- 断言是邪恶的吗?
- 下面这些短语在c++中是什么意思:0 -,default-和value-initialization?
- 在STL地图中,使用map::insert比[]更好吗?
- C++ Linux的想法?
- 如何为Fedora安装g++ ?
- Std::cin输入空格?
- c++标准是否要求iostreams的性能很差,或者我只是在处理一个糟糕的实现?
- gcc在哪里查找C和c++头文件?