10

So I am new to the concept of virtual functions in C++, and threads like this do a good job of selling this concept. Ok I am convinced.

But why are virtual functions called 'virtual'? I mean such functions are as 'concrete' as usual functions / methods aren't they? If someone could explain the choice of the word 'virtual' for naming this concept, that would be great.

Community
  • 1
  • 1
curiousexplorer
  • 1,217
  • 1
  • 17
  • 24
  • @H2CO3 that's not overloaded in any sense. Overloading is when a function with the same name accepts different arguments. – rubenvb Jul 21 '12 at 17:48
  • @rubenvb correct -- I'll update my comment. –  Jul 21 '12 at 17:49
  • @H2CO3 Great!. The reason I mentioned it is because one has runtime overhead and the other doesn't `;)`. – rubenvb Jul 21 '12 at 17:51
  • @rubenvb so I'll copy my comment as an answer :) –  Jul 21 '12 at 17:52
  • 1
    @H2CO3 oh and `override` is a new ---keyword--- "identifier with a special meaning when appearing in a certain context" in C++11. – rubenvb Jul 21 '12 at 18:01
  • @rubenvb that may be true, but then what word do I use for a function that can be, well, overridden by subclasses? –  Jul 21 '12 at 18:02

2 Answers2

12

Virtuality, the quality of having the attributes of something without sharing its (real or imagined) physical form

^ http://en.wikipedia.org/wiki/Virtual

A C++ virtual function appears to be an ordinary function ("having the attributes of"), but the implementation that will be called is not shared out via the declaration, or for that matter via an inline implementation.

Cheers and hth. - Alf
  • 142,714
  • 15
  • 209
  • 331
  • Thank you for the answer. Could you explain what you mean by 'not shared out via the declaration' ? – curiousexplorer Jul 21 '12 at 17:56
  • "not shared out via the declaration" means that the information is not provided. the declaration gives no information about which implementation will be called by any given (virtual) call of the function. it's possible to qualify a call so that the call isn't virtual, e.g. `p->SomeClass::aMethod()`, but even in this case the `SomeClass`' declaration of `aMethod` doesn't tell you which implementation will be called. there is one exception, namely a C++11 `final` method, which can't be overridden, when the `final` version is also the original introduction of the method. but that's patological... – Cheers and hth. - Alf Jul 21 '12 at 18:08
  • 1
    This statement about giving no information also applies to non-virtual non-inline member functions, and indeed most functions. Virtual functions in C++ refer specifically to dispatch on the run-time type of the object. – Bryan Jul 21 '12 at 20:30
  • @Bryan: your second statement is correct. your first statement is therefore incorrect. – Cheers and hth. - Alf Jul 21 '12 at 20:51
  • I think you're reading more into this than is actually in the words. If I declare a C function implemented in a DLL, then the declaration tells you nothing about the implementation; it depends which DLL is loaded. Yet this does not involve virtual functions. – Bryan Jul 21 '12 at 21:54
  • @Bryan: DLLs and other dynamic libraries like Unix SO are not part of standard C++. If by "reading into this" you mean the word `virtual`, well it's the common meaning. That's why I just quoted Wikipedia on it. – Cheers and hth. - Alf Jul 21 '12 at 23:09
11

'virtual function' means a member function where the specific implementation will depend on the type of the object it is called upon, at run-time. The compiler and run-time support of the language contrive to make this happen.

The keyword 'virtual' in C++ was taken from Simula, which had impressed Bjarne Stroustrup. Lots more background here: Pure virtual or abstract, what's in a name?

.. the SIMULA 67 Common Base Language (1970) .. seems to be the first language to introduce OO keywords as class, object, and also virtual as a formal concept.

Community
  • 1
  • 1
Bryan
  • 11,398
  • 3
  • 53
  • 78