Questions tagged [virtual-functions]
The virtual-functions tag has no summary.
                23 questions
            
            
            
                0
            
            votes
        
        
            
                2
            
            answers
        
        
            
                272
            
            views
        
        
            
            
        Help with optimizing virtual method
                    I am developing a key / value database
https://github.com/nmmmnu/HM4
Currently I have following commands
XNSUM - sum keys with same prefix
XRSUM - sum keys from some range
XNCOUNT - count keys with ...
                
            
       
        
            
                2
            
            votes
        
        
            
                3
            
            answers
        
        
            
                1k
            
            views
        
        
            
            
            
        Why do we need factories in the first place?
                    I went through various blogs but nowhere I can find the reason of having virtual constructors in C++.
Why is virtual constructor needed and lets say if we go through the wrong way of creating virtual ...
                
            
       
        
            
                2
            
            votes
        
        
            
                3
            
            answers
        
        
            
                678
            
            views
        
        
            
            
        Object Oriented Programming - what is the best way to add new parameters?
                    I have a doubt about what would be the right OOP approach for implementing classes that do similar stuff with different parameters.
To provide a simple example, I would use two polynomial classes, ...
                
            
       
        
            
                -1
            
            votes
        
        
            
                1
            
            answer
        
        
            
                100
            
            views
        
        
            
            
            
        What do you call a function that properly remains undefined?
                    Backstory:
I have subclasses that are supposed to override and define various functions, but not neccessarily all of them. They can't remain purely virtual though for obvious reasons, and so I am ...
                
            
       
        
            
                3
            
            votes
        
        
            
                3
            
            answers
        
        
            
                4k
            
            views
        
        
            
            
        True cost of virtual dispatch in C++ - when stop using it?
                    What I've learned so far as a programmer has lead me to think that the easiest way to write large scale software is to use a lot of interfaces - this makes things very easy to isolate and test. In ...
                
            
       
        
            
                0
            
            votes
        
        
            
                1
            
            answer
        
        
            
                111
            
            views
        
        
            
            
            
        How to define values dependant on the derived type?
                    My base class must provide an interface to get a value that is dependent solely on the type of the derived class. I can think of two ways of implementing this:
Solution A, virtual functions:
class ...
                
            
       
        
            
                24
            
            votes
        
        
            
                4
            
            answers
        
        
            
                7k
            
            views
        
        
            
            
            
        Never make public members virtual/abstract - really?
                    Back in the 2000s a colleague of mine told me that it is an anti-pattern to make public methods virtual or abstract.
For example, he considered a class like this not well designed:
public abstract ...
                
            
       
        
            
                7
            
            votes
        
        
            
                2
            
            answers
        
        
            
                4k
            
            views
        
        
            
        C++ : What is the order of function pointers inside vtable?
                    In this answer to "In C++ why and how are virtual functions slower?",
the author mentions below point: 
"Get the right function address from the vtable into a register (the index where the correct ...
                
            
       
        
            
                1
            
            vote
        
        
            
                1
            
            answer
        
        
            
                325
            
            views
        
        
            
            
        Differences between branching and virtual methods
                    I was trying to find more info on the matter but could only find this:
In C++ why and how are virtual functions slower?
The answer says that the virtual call "Get[s] the right function address from ...
                
            
       
        
            
                3
            
            votes
        
        
            
                3
            
            answers
        
        
            
                4k
            
            views
        
        
            
            
            
        Do any compilers do this optimization for virtual calls?
                    This just came to mind, and not really sure how to search for this.
Let's say you have the following classes
class A
{
public:
    virtual void Foo() = 0;
    virtual void ManyFoo(int N) 
    {
    ...
                
            
       
        
            
                2
            
            votes
        
        
            
                4
            
            answers
        
        
            
                2k
            
            views
        
        
            
            
        Should a base class implement a virtual method for the most common type or derived class?
                    Is it okay to implement a virtual method in a base class because I know that the majority of derived classes will use that particular implementation?
Suppose I have the following class hierarchy (in ...
                
            
       
        
            
                7
            
            votes
        
        
            
                4
            
            answers
        
        
            
                15k
            
            views
        
        
            
            
            
        Is overriding a pure virtual function with default arguments good or bad?
                    Is overriding a pure virtual function with default arguments good or bad?
class Base {
public:
    virtual int func(int i, int j = 10) = 0;
};
class Derived : public Base {
public:
    int func(int ...
                
            
       
        
            
                -9
            
            votes
        
        
            
                1
            
            answer
        
        
            
                974
            
            views
        
        
            
            
        Why runtime ploymorphism is required? [closed]
                    Why runtime ploymorphism is required ? give a real example that "why it came into existence when we have compile time polymorphism??" As mainting the runtime polymorphism requires lot of overhead of ...
                
            
       
        
            
                3
            
            votes
        
        
            
                1
            
            answer
        
        
            
                127
            
            views
        
        
            
            
            
        How is correct function method called at run-time?
                    Suppose we have a base class Aand derived class B.
A implements a virtual method foo() which is overloaded in B.
If we have an object test of type B, then the object would surely contain both ...
                
            
       
        
            
                3
            
            votes
        
        
            
                1
            
            answer
        
        
            
                5k
            
            views
        
        
            
            
            
        What are the consequences of no virtual destructor for this base class?
                    I found this same code here:
https://stackoverflow.com/a/5854862/257299
struct Base { virtual Base& operator+=(int) = 0; };
struct X : Base
{
    X(int n) : n_(n) { }
    X& operator+=(int n)...
                
            
       
         
         
        