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)

1373 questions
1636
votes
28 answers

What is the difference between an abstract method and a virtual method?

What is the difference between an abstract method and a virtual method? In which cases is it recommended to use abstract or virtual methods? Which one is the best approach?
Moran Helman
  • 17,632
  • 4
  • 21
  • 22
1451
votes
26 answers

Why do we need virtual functions in C++?

I'm learning C++ and I'm just getting into virtual functions. From what I've read (in the book and online), virtual functions are functions in the base class that you can override in derived classes. But earlier in the book, when learning about…
Jake Wilson
  • 78,902
  • 83
  • 230
  • 344
1371
votes
18 answers

Virtual member call in a constructor

I'm getting a warning from ReSharper about a call to a virtual member from my objects constructor. Why would this be something not to do?
JasonS
  • 22,382
  • 9
  • 37
  • 46
358
votes
8 answers

Can I call a base class's virtual function if I'm overriding it?

Say I have classes Foo and Bar set up like this: class Foo { public: int x; virtual void printStuff() { std::cout << x << std::endl; } }; class Bar : public Foo { public: int y; void printStuff() { // I…
Alex
  • 13,729
  • 13
  • 55
  • 88
334
votes
13 answers

Can a class member function template be virtual?

I have heard that C++ class member function templates can't be virtual. Is this true? If they can be virtual, what is an example of a scenario in which one would use such a function?
WannaBeGeek
  • 3,446
  • 3
  • 15
  • 9
262
votes
22 answers

Why do we not have a virtual constructor in C++?

Why does C++ not have a virtual constructor?
Arjun
  • 2,727
  • 4
  • 16
  • 5
250
votes
14 answers

Calling virtual functions inside constructors

Suppose I have two C++ classes: class A { public: A() { fn(); } virtual void fn() { _n = 1; } int getn() { return _n; } protected: int _n; }; class B : public A { public: B() : A() {} virtual void fn() { _n = 2; } }; If I write the…
David Coufal
  • 5,063
  • 4
  • 25
  • 30
247
votes
5 answers

Is the 'override' keyword just a check for a overridden virtual method?

As far as I understand, the introduction of override keyword in C++11 is nothing more than a check to make sure that the function being implemented is the overrideing of a virtual function in the base class. Is that it?
aiao
  • 4,261
  • 3
  • 20
  • 43
240
votes
9 answers

C++ "virtual" keyword for functions in derived classes. Is it necessary?

With the struct definition given below... struct A { virtual void hello() = 0; }; Approach #1: struct B : public A { virtual void hello() { ... } }; Approach #2: struct B : public A { void hello() { ... } }; Is there any difference…
Anarki
  • 4,123
  • 4
  • 17
  • 9
175
votes
13 answers

Are inline virtual functions really a non-sense?

I got this question when I received a code review comment saying virtual functions need not be inline. I thought inline virtual functions could come in handy in scenarios where functions are called on objects directly. But the counter-argument came…
aJ.
  • 32,074
  • 21
  • 79
  • 124
173
votes
6 answers

Can you write virtual functions / methods in Java?

Is it possible to write virtual methods in Java, as one would do in C++? Or, is there a proper Java approach which you can implement that produces similar behavior? Could I please have some examples?
yonatan
  • 1,933
  • 3
  • 14
  • 8
130
votes
15 answers

Virtual functions and performance - C++

In my class design, I use abstract classes and virtual functions extensively. I had a feeling that virtual functions affects the performance. Is this true? But I think this performance difference is not noticeable and looks like I am doing…
Navaneeth K N
  • 14,484
  • 33
  • 119
  • 177
122
votes
12 answers

How are virtual functions and vtable implemented?

We all know what virtual functions are in C++, but how are they implemented at a deep level? Can the vtable be modified or even directly accessed at runtime? Does the vtable exist for all classes, or only those that have at least one virtual…
Brian R. Bondy
  • 314,085
  • 114
  • 576
  • 619
113
votes
8 answers

Where do "pure virtual function call" crashes come from?

I sometimes notice programs that crash on my computer with the error: "pure virtual function call". How do these programs even compile when an object cannot be created of an abstract class?
Brian R. Bondy
  • 314,085
  • 114
  • 576
  • 619
111
votes
9 answers

What is the performance cost of having a virtual method in a C++ class?

Having at least one virtual method in a C++ class (or any of its parent classes) means that the class will have a virtual table, and every instance will have a virtual pointer. So the memory cost is quite clear. The most important is the memory…
MiniQuark
  • 40,659
  • 30
  • 140
  • 167
1
2 3
91 92