English 中文(简体)
我们为什么使用虚拟关键字(从词源学角度)?[已关闭]
原标题:Why do we use the virtual keyword (etymologically)? [closed]

这个问题似乎不是关于帮助中心

Closed 8 years ago.

大多数修饰语对我来说都是有意义的——抽象的、受保护的等等。但“虚拟的”似乎是一个令人困惑的任意选择,因为什么是真正的“可重写的”。

计算中的虚拟(虚拟机、虚拟内存)似乎意味着某种东西的抽象表示。我想这源于光学,其中虚拟图像(通常在*vert*ed中)本身来自中世纪拉丁语virtualālis,意思是“有效的”。

因此,在我看来,如果计算中的虚拟方法是压倒一切的方法,而不是被压倒的方法,那将更有意义。

也许我错过了虚拟的一些基本内容,有人能解释为什么选择它吗?

最佳回答

我倾向于认为“虚拟”一词的意思是不真实的。例如,就虚拟内存而言,它在某个时刻将映射到真实内存,但在需要映射之前,它实际上是一个占位符。

类似地,虚拟函数是一个占位符,它在某个点映射到实函数。

即使你使用(有点幼稚的)“假装”的定义,它也是有道理的:

virtual/pretend machine
virtual/pretend memory
virtual/pretend function

在所有这些情况下,“虚拟”都是不真实的东西。它们的使用被转化为真实的东西(模拟机器,通过MMU映射内存地址,调用特定于类的实际函数而不是虚拟函数,等等)。

问题回答

当你说,

“虚拟”似乎是一个令人困惑的任意选择,因为什么是真正的“可重写”

我同意overridable可能是一个更好的关键词,但我不认为virtual是任意的。我认为使用它有一个很好的、历史上合理的理由。

一些C++示例:

class C {
    virtual int x() { ... }
    ...
}

C *cp;

...

cp->x();

cp->x() will call real code, so I don t think abstract is the right word. But which code will it call? Just because cp is a pointer to C doesn t mean it couldn t be pointing to something that inherits from C.

See, the call is virtualized. Just like virtual memory addresses are mapped to real memory addresses, virtual function calls are mapped to real functions calls, via the virtual method table.

根据谷歌的说法,虚拟的意思是,“几乎或接近所描述的,但不完全或根据严格的定义”。我认为这是一个相当不错的形容词,考虑到虚拟方法调用几乎就像真实的方法调用,除了一些重定向。

Now, imagine when virtual method calls are a brand new idea. You could imagine that, in all the heady excitement of creating a new language that has them built right in (!!!), one would be naturally drawn to using that keyword.

Poor C::x() is an innocent bystander in all this excitement. Using the virtual keyword tells the compiler that calls to the method will be virtualized, but clearly there s nothing virtual or abstract about C::x() itself. It s just the poor sucker who gets stuck being the default method in the method table.

Now this is just conjecture, but perhaps there s a reason that C++ uses the term "pure virtual" to describe a method that has to overridden. Maybe concrete objects of type C don t use virtual method calls, even for "virtual" methods. Pure virtual methods, of course, would always have to be called through a virtual method table.

考虑以下代码行:

base.Method();

其中,base是base或其子类之一的实例化。

如果Method()是一个虚拟方法,那么我们不会直接调用函数Method()。相反,我们被“重定向”到实际功能,这取决于基础是什么。

因此,虚拟这个词并不是试图描述基类中声明的函数本身。它实际上描述了我们想要抽象函数调用的事实。





相关问题
virtual column for drop down in the ruby on rails

How to place a drop down for the virtual column, in the ruby on rails. The virutal column does not exist in my table , but I want to get the value from the drop down ,when user saves the data. for ...

C++ : implications of making a method virtual

Should be a newbie question... I have existing code in an existing class, A, that I want to extend in order to override an existing method, A::f(). So now I want to create class B to override f(), ...

C++ static virtual members?

Is it possible in C++ to have a member function that is both static and virtual? Apparently, there isn t a straightforward way to do it (static virtual member(); is a compile error), but is there at ...

C++ collection of abstract base classes

how can I create STL collection of classes which implement abstract base class using the base class as collection value, without using pointers? is there something in Boost that allows me to ...

How to design a C++ API for binary compatible extensibility

I am designing an API for a C++ library which will be distributed in a dll / shared object. The library contains polymorhic classes with virtual functions. I am concerned that if I expose these ...

热门标签