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).
                                	
	virtual-functions
    
                            
                        
                    
            1,536
            questions
        
        
            1719
            votes
        
        
            27
            answers
        
        
            652k
            views
        
    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?
            
        
       
    
            1649
            votes
        
        
            28
            answers
        
        
            697k
            views
        
    Why do we need virtual functions in C++?
                From what I've read, virtual functions are functions in the base class that you can override in its derived classes.
But earlier, when learning about basic inheritance, I was able to override base ...
            
        
       
    
            1489
            votes
        
        
            18
            answers
        
        
            223k
            views
        
    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?
            
        
       
    
            420
            votes
        
        
            8
            answers
        
        
            317k
            views
        
    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
{...
            
        
       
    
            389
            votes
        
        
            15
            answers
        
        
            286k
            views
        
    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?
            
        
       
    
            334
            votes
        
        
            15
            answers
        
        
            157k
            views
        
    Why is a call to a virtual member function in the constructor a non-virtual call?
                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() {}
  ...
            
        
       
    
            310
            votes
        
        
            23
            answers
        
        
            282k
            views
        
    Why do we not have a virtual constructor in C++?
                Why does C++ not have a virtual constructor?
            
        
       
    
            277
            votes
        
        
            6
            answers
        
        
            58k
            views
        
    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 ...
            
        
       
    
            271
            votes
        
        
            4
            answers
        
        
            354k
            views
        
    Difference between virtual and abstract methods [duplicate]
                Here is some code from MSDN:
// compile with: /target:library 
public class D
{
    public virtual void DoWork(int i)
    {
        // Original implementation.
    }
}
public abstract class E : D
{
 ...
            
        
       
    
            268
            votes
        
        
            9
            answers
        
        
            104k
            views
        
    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 {
    ...
            
        
       
    
            194
            votes
        
        
            6
            answers
        
        
            260k
            views
        
    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?
            
        
       
    
            193
            votes
        
        
            13
            answers
        
        
            101k
            views
        
    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 ...
            
        
       
    
            148
            votes
        
        
            15
            answers
        
        
            85k
            views
        
    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 ...
            
        
       
    
            138
            votes
        
        
            12
            answers
        
        
            72k
            views
        
    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, ...
            
        
       
    
            131
            votes
        
        
            9
            answers
        
        
            47k
            views
        
    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 ...
            
        
       
    
            129
            votes
        
        
            8
            answers
        
        
            126k
            views
        
    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?
            
        
       
    
            125
            votes
        
        
            3
            answers
        
        
            111k
            views
        
    Why is a call to a shadowing non-virtual member function in the derived class not calling the base class member function?
                Let's assume this scenario in Visual C++ 2010:
#include <iostream>
using namespace std;
struct Base {
    void Display() {
        cout << "Base: Non-virtual display." << ...
            
        
       
    
            123
            votes
        
        
            8
            answers
        
        
            150k
            views
        
    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?
            
        
       
    
            119
            votes
        
        
            6
            answers
        
        
            86k
            views
        
    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 ...
            
        
       
    
            108
            votes
        
        
            12
            answers
        
        
            35k
            views
        
    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?
            
        
       
    
            107
            votes
        
        
            10
            answers
        
        
            15k
            views
        
    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 ...
            
        
       
    
            105
            votes
        
        
            10
            answers
        
        
            118k
            views
        
    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 ...
            
        
       
    
            93
            votes
        
        
            3
            answers
        
        
            67k
            views
        
    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)?
            
        
       
    
            79
            votes
        
        
            5
            answers
        
        
            85k
            views
        
    virtual assignment operator C++
                Assignment Operator in C++ can be made virtual. Why is it required? Can we make other operators virtual too?
            
        
       
    
            77
            votes
        
        
            1
            answer
        
        
            51k
            views
        
    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 ...
            
        
       
    
            76
            votes
        
        
            8
            answers
        
        
            10k
            views
        
    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 ...
            
        
       
    
            75
            votes
        
        
            5
            answers
        
        
            37k
            views
        
    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 ...
            
        
       
    
            74
            votes
        
        
            11
            answers
        
        
            30k
            views
        
    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 ...
            
        
       
    
            69
            votes
        
        
            2
            answers
        
        
            80k
            views
        
    Can we have a static virtual functions? If not, then WHY? [duplicate]
                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 ...
            
        
       
    
            68
            votes
        
        
            7
            answers
        
        
            27k
            views
        
    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:
        ...
            
        
       
    
            60
            votes
        
        
            10
            answers
        
        
            53k
            views
        
    Practical usage of virtual functions in c#
                What 's the practical usage of virtual functions in c#?
            
        
       
    
            60
            votes
        
        
            4
            answers
        
        
            36k
            views
        
    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 ...
            
        
       
    
            55
            votes
        
        
            11
            answers
        
        
            8k
            views
        
    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 ...
            
        
       
    
            55
            votes
        
        
            7
            answers
        
        
            25k
            views
        
    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?
            
        
       
    
            54
            votes
        
        
            3
            answers
        
        
            10k
            views
        
    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 ...
            
        
       
    
            50
            votes
        
        
            16
            answers
        
        
            18k
            views
        
    Performance penalty for working with interfaces in C++?
                Is there a runtime performance penalty when using interfaces (abstract base classes) in C++?
            
        
       
    
            50
            votes
        
        
            11
            answers
        
        
            7k
            views
        
    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 ...
            
        
       
    
            49
            votes
        
        
            3
            answers
        
        
            2k
            views
        
    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 ...
            
        
       
    
            47
            votes
        
        
            2
            answers
        
        
            5k
            views
        
    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 ...
            
        
       
    
            45
            votes
        
        
            4
            answers
        
        
            107k
            views
        
    Why use virtual functions? [duplicate]
                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 ...
            
        
       
    
            45
            votes
        
        
            3
            answers
        
        
            45k
            views
        
    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 ...
            
        
       
    
            44
            votes
        
        
            9
            answers
        
        
            20k
            views
        
    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) ...
            
        
       
    
            42
            votes
        
        
            4
            answers
        
        
            19k
            views
        
    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?
            
        
       
    
            40
            votes
        
        
            9
            answers
        
        
            18k
            views
        
    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 ...
            
        
       
    
            40
            votes
        
        
            9
            answers
        
        
            8k
            views
        
    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 ...
            
        
       
    
            38
            votes
        
        
            4
            answers
        
        
            15k
            views
        
    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 = ...
            
        
       
    
            38
            votes
        
        
            1
            answer
        
        
            6k
            views
        
    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 ...
            
        
       
    
            37
            votes
        
        
            4
            answers
        
        
            9k
            views
        
    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
};
...
            
        
       
    
            35
            votes
        
        
            5
            answers
        
        
            24k
            views
        
    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 ...
            
        
       
    
            34
            votes
        
        
            3
            answers
        
        
            12k
            views
        
    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
...