Questions tagged [virtual-functions]

The tag has no usage guidance.

Filter by
Sorted by
Tagged with
18
votes
3answers
4k 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 ...
4
votes
2answers
2k 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 ...
0
votes
1answer
122 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 ...
2
votes
3answers
1k 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
4answers
348 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 ...
3
votes
3answers
6k views

Is overriding a pure virtual function with default arguments is good or bad?

Is overriding a pure virtual function with default arguments is good or bad? class Base { public: virtual int func(int i, int j = 10) = 0; }; class Derived : public Base { public: int func(...
-9
votes
1answer
610 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
1answer
110 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 ...
2
votes
1answer
1k 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)...
2
votes
4answers
1k views

Why do I need to declare virtual functions as such?

Example: We have a base class Base and three subclasses which all implement their own version of doSomething(). In an intermediate function f(Base b), we want to call the relevant version of ...
3
votes
3answers
2k views

When to mark a function as virtual?

I'm trying to understand the idiomatic way to code. I'm using gmock to unit test the components I write. Gmock requires methods to be virtual to be able to mock but the class I'm trying to mock has a ...
0
votes
1answer
112 views

Is it possible to detect misuse of passing self type argument in compile time?

I just hit the very basic problem in OOP and I cannot see any working solution except postponing appropriate check until run time. It is pretty clear notion of an action "do something with value of ...
4
votes
1answer
12k views

How does the base class non-virtual function get called when derived class object is assigned to base class?

#include <iostream> class Base { private: int b_value; public: void my_func() {std::cout << "This is Base's non-virutal my_func()" << std::endl; } virtual void ...
1
vote
1answer
107 views

Pass data into a constructor or instead create virtual methods?

In an object oriented module describing a database, should I pass DB description data structures to the constructor in the constructors of derived classes, or should I instead create ("virtual" that ...
1
vote
1answer
71 views

Virtual method returning a unique collection - how to ensure and hint?

I have a virtual method that returns a collection of items that must be unique. I want to make sure that it will be obvious when overriding the method. What is the best way to do this? Here is an ...
48
votes
8answers
46k views

When NOT to use virtual destructors?

I believed I searched many times about virtual destructors, most mention the purpose of virtual destructors, and why you need virtual destructors. Also I think in most cases destructors need to be ...
31
votes
1answer
5k views

Why does C++ not have a “pure” keyword for virtual functions?

I have always wondered why we code virtual void MyFunction() = 0; and not pure virtual void MyFunction(); Is there a reference for the basis of this decision?