什么是最简洁但准确的方法来描述什么是C ++中的虚拟函数? [英] What is the most concise yet accurate way to describe what a virtual function is in C++?

查看:112
本文介绍了什么是最简洁但准确的方法来描述什么是C ++中的虚拟函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

被要求描述一个虚拟函数似乎是在评估基本C ++知识的访谈中最常见的问题之一。然而,经过几年的编程C ++,我仍然有不舒服的感觉,我不真正明白如何最好地定义他们是什么。



如果我咨询维基百科,我参见虚函数的定义是:


在面向对象编程中,虚函数或虚方法是一个函数或方法可以通过具有相同签名的函数在继承类中重写。


这个定义看起来简单而优雅,但对我来说,似乎并没有捕捉到C ++中虚拟函数的概念,因为一个非虚函数肯定也可以在一个继承类中被具有相同签名的函数重写。



如果我被要求描述一个虚拟函数是什么非正式的,我说一些关于指针,如这是一个方法,当你通过基类指针调用它的时候,它的版本定义在派生类被调用,如果指针实际指向一个派生类的实例。这似乎不是一个非常优雅的概念的描述。我知道人们说这是怎么在C ++中实现的多态性(多态性,据我所知,大致是将对象组织成层次结构的整个想法),但我不知道一个鸽友的方式来理解或解释



我想我很困惑,对于虚函数的指针描述是否是它们定义的基础,或者只是偶尔在C ++中实现它们。

解决方案

我一直认为这个引用描述了虚函数的本质:


虚函数是一种定义一系列相关行为的方法,这些行为可以由实体定制,这些实体实际上必须做这些行为。


< blockquote>

如果忽略虚函数的所有C ++ - isms - 如何使虚函数启用 dynamic_cast 类类型,如何通过指针访问它们,如何虚拟析构函数完全不同于虚拟非析构函数等。 - 我认为上面的语句是虚拟函数的核心。 / p>

我喜欢这个语句的主要原因是它以一种将它们与编程解耦的方式来描述虚拟函数。你可以通过给出一些具体的类比来解释虚拟函数给非技术人员使用这个定义。例如,打开灯光的想法可以被认为是一个虚拟功能,因为打开灯光时发生的实际机制完全取决于您使用的特定光源(白炽灯?荧光灯? ),但是每种情况下的概念想法是相同的。当然,这不是一个完美的类比,但我认为它得到了很好的点。



更一般来说,IMHO,如果你被要求描述非正式的东西,尝试尽可能远离你使用的特定编程语言,如果可能,从所有的计算机。尝试考虑概念适用的最一般的设置,然后在该级别描述它。然后再次,我教导介绍性CS课程,所以我有一个偏见这个领域,所以我不知道这是如何适用这是在面试的设置。 : - )


Being asked to describe what a virtual function is seems to be one of the most common questions in interviews assessing basic C++ knowledge. However, after several years programming C++, I still have the uncomfortable feeling that I don't really understand how best to define what they are.

If I consult wikipedia, I see the definition of virtual function is:

"In object-oriented programming, a virtual function or virtual method is a function or method whose behaviour can be overridden within an inheriting class by a function with the same signature"

This definition seems simple and elegant, and not C++ specific. But to me it doesn't seem to capture the concept of a virtual function in C++, since surely a non-virtual function can also be overridden within an inheriting class by a function with the same signature.

If I'm asked to describe what a virtual function is informally, I say something about pointers like "it's a method such that when you call it via a base class pointer, the version of it defined in the derived class is called instead, if the pointer actually points to an instance of a derived class". This doesn't seem like a very elegant description of the concept. I know that people say this is how "polymorphism" is achieved in C++ (polymorphism, as far as I understand, roughly being the whole idea of organizing objects into hierarchies), but I don't know of a fancier way to understand or explain the mechanism than to go through the example with the pointers.

I guess I'm confused about whether the "pointer" description of virtual functions is something fundamental to their definition, or just something incidental to their implementation in C++.

解决方案

I've always thought that this quote captures the essence of virtual functions:

A virtual function is a way of defining a family of related behaviors that can be customized by the entities that actual have to do those behaviors.

If you ignore all the C++-isms of virtual functions - how having virtual functions enables dynamic_cast to work for objects of the class type, how they're only treated virtually if accessed through a pointer, how virtual destructors are completely different from virtual non-destructors, etc. - I think that the above statement gets at the heart of what virtual functions are all about.

The main reason I like this statement is that it describes virtual functions in a way that decouples them from programming. You could explain virtual functions to a non-technical person using this definition by giving some concrete analogies. For example, the idea of "turning on a light" could be thought of as a virtual function because the actual mechanics of what happens when you turn on a light depends entirely on the particular light you're using (incandescent? fluorescent? LED?), but the conceptual idea is the same in each case. Of course, it's not a perfect analogy, but I think it gets the point across well enough.

More generally, IMHO, if you're ever asked to describe something informally, try as much as possible to distance yourself from the particular programming language you're using and, if at all possible, from computers at all. Try to think of the most general setting in which the concept applies, and then describe it at that level. Then again, I teach introductory CS courses, and so I have a bit of a bias toward this domain, so I have no idea how applicable this is in a job interview setting. :-)

这篇关于什么是最简洁但准确的方法来描述什么是C ++中的虚拟函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆