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
127
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
  • 339,232
  • 124
  • 596
  • 636
119
votes
6 answers

Why are C# interface methods not declared abstract or virtual?

C# methods in interfaces are declared without using the virtual keyword, and overridden in the derived class without using the override keyword. Is there a reason for this? I assume that it is just a language convenience, and obviously the CLR…
Robert Harvey
  • 178,213
  • 47
  • 333
  • 501
116
votes
8 answers

How to implement virtual methods in Python?

I know virtual methods from PHP or Java. How can they be implemented in Python? Or have I to define an empty method in an abstract class and override it?
Meloun
  • 13,601
  • 17
  • 64
  • 93
108
votes
12 answers

When should you not use virtual destructors?

Is there ever a good reason to not declare a virtual destructor for a class? When should you specifically avoid writing one?
Mag Roader
  • 6,850
  • 8
  • 32
  • 27
107
votes
10 answers

Why C# implements methods as non-virtual by default?

Unlike Java, why does C# treat methods as non-virtual functions by default? Is it more likely to be a performance issue rather than other possible outcomes? I am reminded of reading a paragraph from Anders Hejlsberg about several advantages the…
Burcu Dogan
  • 9,153
  • 4
  • 34
  • 34
105
votes
10 answers

Safely override C++ virtual functions

I have a base class with a virtual function and I want to override that function in a derived class. Is there some way to make the compiler check if the function I declared in the derived class actually overrides a function in the base class? I…
sth
  • 222,467
  • 53
  • 283
  • 367
91
votes
3 answers

C++ virtual function return type

Is it possible for an inherited class to implement a virtual function with a different return type (not using a template as return)?
Bob
  • 10,741
  • 27
  • 89
  • 143
78
votes
5 answers

virtual assignment operator C++

Assignment Operator in C++ can be made virtual. Why is it required? Can we make other operators virtual too?
Kazoom
  • 5,659
  • 16
  • 56
  • 69
76
votes
8 answers

Should I mark all methods virtual?

In Java you can mark method as final to make it impossible to override. In C# you have to mark method as virtual to make it possible to override. Does it mean that in C# you should mark all methods virtual (except a few ones that you don't want to…
Georgii Oleinikov
  • 3,865
  • 3
  • 27
  • 27
74
votes
5 answers

Does final imply override?

As I understand it, the override keyword states that a given declaration implements a base virtual method, and the compilation should fail if there is no matching base method found. My understanding of the final keyword is that it tells the compiler…
quant
  • 21,507
  • 32
  • 115
  • 211
74
votes
11 answers

What's the point of a final virtual function?

Wikipedia has the following example on the C++11 final modifier: struct Base2 { virtual void f() final; }; struct Derived2 : Base2 { void f(); // ill-formed because the virtual function Base2::f has been marked final }; I don't understand…
fredoverflow
  • 256,549
  • 94
  • 388
  • 662
71
votes
1 answer

Should I default virtual destructors?

I have an abstract class that is declared as follow: class my_type { public: virtual ~my_type() = default; virtual void do_something() = 0; }; Is it considered good practice to declare the destructor like this, with the default keyword? Is…
Humam Helfawi
  • 19,566
  • 15
  • 85
  • 160
67
votes
2 answers

Can we have a static virtual functions? If not, then WHY?

Possible Duplicate: C++ static virtual members? Can we have a static virtual functions? If not, then WHY? class X { public: virtual static void fun(){} // Why we cant have static virtual function in C++? };
Jatin
  • 1,857
  • 4
  • 24
  • 37
66
votes
7 answers

Overriding public virtual functions with private functions in C++

Is there is any reason to make the permissions on an overridden C++ virtual function different from the base class? Is there any danger in doing so? For example: class base { public: virtual int foo(double) = 0; } class child : public…
Ben Martin
  • 1,470
  • 2
  • 13
  • 16
60
votes
10 answers

Practical usage of virtual functions in c#

What 's the practical usage of virtual functions in c#?
odiseh
  • 25,407
  • 33
  • 108
  • 151