Questions tagged [virtual-functions]

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 concept is a very important part of the polymorphism portion of object-oriented programming (OOP).

The concept of the virtual function solves the following problem:

In OOP when a derived class inherits a base class, an object of the derived class may be referred to (or cast) as either being the base class type or the derived class type. If there are base class methods overridden by the derived class, the method call behavior is ambiguous.

The distinction between virtual and non-virtual resolves this ambiguity. If the function in question is designated virtual in the base class then the derived class' function would be called (if it exists). If it is not virtual, the base class' function would be called.

Virtual functions overcome the problems with the type-field solution by allowing the programmer to declare functions in a base class that can be redefined in each derived class.

In C++ virtual methods are declared by prepending the virtual keyword to the function's declaration.

Source: Wikipedia (Virtual function)

1509 questions
35
votes
4 answers

Return Type Covariance with Smart Pointers

In C++ we can do this: struct Base { virtual Base* Clone() const { ... } virtual ~Base(){} }; struct Derived : Base { virtual Derived* Clone() const {...} //overrides Base::Clone }; However, the following won't do the same trick: struct…
Armen Tsirunyan
  • 130,161
  • 59
  • 324
  • 434
35
votes
4 answers

Performance of Expression.Compile vs Lambda, direct vs virtual calls

I'm curious how performant the Expression.Compile is versus lambda expression in the code and versus direct method usage, and also direct method calls vs virtual method calls (pseudo code): var foo = new Foo(); var iFoo =…
Serge Semenov
  • 9,232
  • 3
  • 23
  • 24
34
votes
2 answers

Is there a pure virtual function in the C++ Standard Library?

In Nicola Gigante's lecture in 2015, he mentions (at the beginning) that there are no pure virtual functions in the Standard Library (or he's not aware of any). I believe that Alex Stepanov was against this language feature but since the initial STL…
34
votes
3 answers

overriding protected internal with protected!

This is an extension for this question asked an hour ago. We cannot modify the access modifiers, when overriding a virtual method in derived class. Consider Control class in System.Web.UI namespace public class Control : IComponent,…
Asad
  • 21,468
  • 17
  • 69
  • 94
34
votes
5 answers

What are the differences between overriding virtual functions and hiding non-virtual functions?

Given the following code fragment, what are the differences in the function calls? What is function hiding? What is function overriding? How do they relate to function overloads? What is the difference between the two? I couldn't find a good…
Jed Schaaf
  • 1,045
  • 1
  • 10
  • 19
33
votes
3 answers

Why can't virtual functions use return type deduction?

n3797 says: § 7.1.6.4/14: A function declared with a return type that uses a placeholder type shall not be virtual (10.3). Therefore the following program is ill-formed: struct s { virtual auto foo() { } }; All I can find for the…
user3920237
33
votes
8 answers

Virtual Methods or Function Pointers

When implementing polymorphic behavior in C++ one can either use a pure virtual method or one can use function pointers (or functors). For example an asynchronous callback can be implemented by: Approach 1 class Callback { public: Callback(); …
doron
  • 27,972
  • 12
  • 65
  • 103
30
votes
5 answers

Override a member function with different return type

Consider the example below: #include using namespace std; class base { public: virtual int func() { cout << "vfunc in base class\n"; return 0; } }; class derived: public base { public: …
nitin_cherian
  • 6,405
  • 21
  • 76
  • 127
30
votes
3 answers

C++: Pointer to monomorphic version of virtual member function?

In C++, it's possible to get a pointer to a (non-static) member function of a class, and then later invoke it on an object. If the function was virtual, the call is dispatched dynamically depending on the dynamic type of the object. It's also…
glaebhoerl
  • 7,695
  • 3
  • 30
  • 41
30
votes
7 answers

Why I have to redeclare a virtual function while overriding [C++]

#include using namespace std; class Duck { public: virtual void quack() = 0; }; class BigDuck : public Duck { public: // void quack(); (uncommenting will make it compile) }; void BigDuck::quack(){ cout <<…
sud03r
  • 19,109
  • 16
  • 77
  • 96
30
votes
2 answers

How to detect if a method is virtual?

I tried to make a traits to find if a method is virtual: (https://ideone.com/9pfaCZ) // Several structs which should fail depending if T::f is virtual or not. template struct Dvf : T { void f() final; }; template struct Dvo…
Jarod42
  • 203,559
  • 14
  • 181
  • 302
29
votes
6 answers

C++ object size with virtual methods

I have some questions about the object size with virtual. 1) virtual function class A { public: int a; virtual void v(); } The size of class A is 8bytes....one integer(4 bytes) plus one virtual pointer(4 bytes) It's…
skydoor
  • 25,218
  • 52
  • 147
  • 201
28
votes
3 answers

Does it make sense to add final keyword to the virtual function in a class that has no base class (is not derived)

I am reading an awesome awesome C++11 tutorial and the author provides this example while explaining the final keyword: struct B { virtual void f() const final; // do not override virtual void g(); }; struct D : B { void f() const; …
Eduard Rostomyan
  • 7,050
  • 2
  • 37
  • 76
27
votes
4 answers

Why use base class pointers for derived classes

class base{ ..... virtual void function1(); virtual void function2(); }; class derived::public base{ int function1(); int function2(); }; int main() { derived d; base *b = &d; int k = b->function1() // Why use this…
Balaji Sridharan
  • 339
  • 1
  • 5
  • 7
27
votes
1 answer

in c++ when subclassing why sometimes need to add virtual keyword to overridden function?

Why do I sometimes see in C++ examples when talking about subclassing / inheritance, the base class has virtual keyword and sometimes the overridden function has also the virtual keyword, why it's necessary to add to the subclass the virtual key…
user63898
  • 29,839
  • 85
  • 272
  • 514