如果一个函数被定义为虚函数,它究竟意味着什么?它与纯虚函数是相同的吗?
当前回答
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.
其他回答
virtual关键字是如何工作的?
假设人是一个基类,那么印度人就是由人演变而来的。
Class Man
{
public:
virtual void do_work()
{}
}
Class Indian : public Man
{
public:
void do_work()
{}
}
将do_work()声明为virtual仅仅意味着:调用哪个do_work()只在运行时决定。
假设我这样做了,
Man *man;
man = new Indian();
man->do_work(); // Indian's do work is only called.
如果不使用virtual,则由编译器静态确定或静态绑定相同的对象,具体取决于调用的对象。因此,如果Man的对象调用do_work(),则Man的do_work()将被调用,即使它指向一个印度对象
我相信投票最多的答案是误导性的——任何方法,无论是否虚,都可以在派生类中有一个重写的实现。具体到c++,正确的区别是相关函数的运行时(使用virtual时)绑定和编译时(不使用virtual但重写方法且基指针指向派生对象时)绑定。
似乎还有另一个误导性的评论说,
“贾斯汀,‘纯虚拟’只是一个术语(不是关键字,请看我的回答 下面)用来表示“此函数不能由基实现。 类。”
这是错误的! 纯虚函数也可以有一个体并且可以实现!事实上,抽象类的纯虚函数可以被静态调用!两位非常优秀的作者是Bjarne Stroustrup和Stan Lippman....因为语言是他们写的。
纯虚函数
试试这段代码
#include <iostream>
using namespace std;
class aClassWithPureVirtualFunction
{
public:
virtual void sayHellow()=0;
};
class anotherClass:aClassWithPureVirtualFunction
{
public:
void sayHellow()
{
cout<<"hellow World";
}
};
int main()
{
//aClassWithPureVirtualFunction virtualObject;
/*
This not possible to create object of a class that contain pure virtual function
*/
anotherClass object;
object.sayHellow();
}
在类anotherClass中,删除sayhello函数并运行代码。你会得到错误!因为当一个类包含一个纯虚函数时,不能从该类创建对象,并且它是继承的,那么它的派生类必须实现该函数。
虚函数
试试其他代码
#include <iostream>
using namespace std;
class aClassWithPureVirtualFunction
{
public:
virtual void sayHellow()
{
cout<<"from base\n";
}
};
class anotherClass:public aClassWithPureVirtualFunction
{
public:
void sayHellow()
{
cout<<"from derived \n";
}
};
int main()
{
aClassWithPureVirtualFunction *baseObject=new aClassWithPureVirtualFunction;
baseObject->sayHellow();///call base one
baseObject=new anotherClass;
baseObject->sayHellow();////call the derived one!
}
这里sayhello函数在基类中被标记为虚函数。它表示编译器尝试在派生类中搜索函数并实现该函数。如果没有找到,则执行基本的一个。谢谢
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.
虚函数是在基类中声明并由派生类重新定义的成员函数。虚函数是按继承顺序分层的。 当派生类不重写虚函数时,将使用在其基类中定义的函数。
纯虚函数是指相对于基类不包含任何定义的虚函数。 它在基类中没有实现。任何派生类都必须重写此函数。
推荐文章
- 什么是“参数依赖查找”(又名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++头文件?
- 为什么我们需要require require ?