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
58
votes
4 answers

C++ header file and function declaration ending in "= 0"

I have the following code inside the .h file and I'm not sure what does the assignment statement do and how is it called properly? virtual void yield() = 0; I thought that the function returns a value of 0 by default but since this function returns…
Adam
  • 581
  • 1
  • 4
  • 3
55
votes
11 answers

Why not have all the functions as virtual in C++?

I know that virtual functions have an overhead of dereferencing to call a method. But I guess with modern architectural speed it is almost negligible. Is there any particular reason why all functions in C++ are not virtual as in Java? From my…
codeObserver
  • 6,521
  • 16
  • 76
  • 121
54
votes
7 answers

Use-cases of pure virtual functions with body?

I recently came to know that in C++ pure virtual functions can optionally have a body. What are the real-world use cases for such functions?
missingfaktor
  • 90,905
  • 62
  • 285
  • 365
53
votes
3 answers

What is the first (int (*)(...))0 vtable entry in the output of g++ -fdump-class-hierarchy?

For this code: class B1{ public: virtual void f1() {} }; class D : public B1 { public: void f1() {} }; int main () { B1 *b1 = new B1(); D *d = new D(); return 0; } After compilation, the vtable I get with g++…
Aquarius_Girl
  • 21,790
  • 65
  • 230
  • 411
49
votes
11 answers

Alternative virtual function calls implementations?

C++ supports dynamic binding through virtual mechanism. But as I understand the virtual mechanism is an implementation detail of the compiler and the standard just specifies the behaviors of what should happen under specific scenarios. Most…
Alok Save
  • 202,538
  • 53
  • 430
  • 533
49
votes
3 answers

Why do we need to use virtual ~A() = default; instead of virtual ~A() {} in C++11?

In Stack Overflow post Checking the object type in C++11, I have the comment: In C++11 you'll actually want to do virtual ~A() = default; Otherwise, you'll lose the implict move constructors. What is virtual ~A() = default; for? How come implicit…
prosseek
  • 182,215
  • 215
  • 566
  • 871
49
votes
16 answers

Performance penalty for working with interfaces in C++?

Is there a runtime performance penalty when using interfaces (abstract base classes) in C++?
andreas buykx
  • 12,608
  • 10
  • 62
  • 76
47
votes
2 answers

Avoiding the overhead of C# virtual calls

I have a few heavily optimized math functions that take 1-2 nanoseconds to complete. These functions are called hundreds of millions of times per second, so call overhead is a concern, despite the already-excellent performance. In order to keep the…
Haus
  • 1,492
  • 7
  • 23
45
votes
4 answers

Why use virtual functions?

Possible Duplicate: Can someone explain C++ Virtual Methods? I have a question regarding to the C++ virtual functions. Why and when do we use virtual functions? Can anyone give me a real time implementation or use of virtual functions?
haris
  • 2,003
  • 4
  • 25
  • 24
44
votes
3 answers

What if I don't heed the warning "hides inherited member. To make the current member override that implementation...."

This is maybe a fine point, but it concerns the warning that the compiler issues if you do something like: class A { public virtual void F() { } } class B : A { public void F() { } } Then you get the warning: 'EomApp1.B.F()' hides inherited…
Aaron Anodide
  • 16,906
  • 15
  • 62
  • 121
44
votes
9 answers

Ways to detect whether a C++ virtual function has been redefined in a derived class

In brief: From a C++ base-class pointer which points to an instance of a derived class, how can one determine at run-time whether a non-pure virtual function (with an implementation in the base class) has been re-implemented in the derived…
Chris Johnson
  • 10,469
  • 4
  • 31
  • 35
40
votes
9 answers

Template or abstract base class?

If I want to make a class adaptable, and make it possible to select different algorithms from the outside -- what is the best implementation in C++? I see mainly two possibilities: Use an abstract base class and pass concrete object in Use a…
Frank
  • 64,140
  • 93
  • 237
  • 324
40
votes
3 answers

Can virtual functions be constexpr?

Can virtual functions like X::f() in the following code struct X { constexpr virtual int f() const { return 0; } }; be constexpr?
Ralph Tandetzky
  • 22,780
  • 11
  • 73
  • 120
40
votes
9 answers

Can you cache a virtual function lookup in C++?

Say I have a virtual function call foo() on an abstract base class pointer, mypointer->foo(). When my app starts up, based on the contents of a file, it chooses to instantiate a particular concrete class and assigns mypointer to that instance. For…
Joseph Garvin
  • 20,727
  • 18
  • 94
  • 165
38
votes
1 answer

How has CPU architecture evolution affected virtual function call performance?

Years ago I was learning about x86 assembler, CPU pipelining, cache misses, branch prediction, and all that jazz. It was a tale of two halves. I read about all the wonderful advantages of the lengthy pipelines in the processor viz instruction…
spraff
  • 32,570
  • 22
  • 121
  • 229