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
-1
votes
2 answers

Using abstract classes in C++ for functions of higher order

First, I know the answer is somewhere out there, but I've been searching all morning and didn't find it. My question concerns what in Java looks like abstract class AbstractWorker { public abstract int doIt(); } class Thinker extends…
Arne L.
  • 2,194
  • 19
  • 19
-1
votes
3 answers

Is there a way to distinguish between these function calls coming from two templated base classes?

I have the following scenario :- template class Base { public: virtual void someFunc() {} }; class Derived : public Base, public Base { public: virtual void someFunc() { // do something…
owagh
  • 3,428
  • 2
  • 31
  • 53
-2
votes
1 answer

C++ using concrete virtual method of abstract base class

I have a abstract base class with a mixture of pure and concrete virtual methods, e.g.: template < class T > class A { public: A ( ) { } virtual ~A ( ) { } virtual void purefunc( const T & a, T & b ) const = 0; virtual T concretefunc…
Bartel
  • 198
  • 1
  • 7
-2
votes
2 answers

why is virtual dispatch via helper function called?

In the following snippet, foo() is not called directly via the Base class pointer (so that dynamic binding kicks in), but is called from a member function. Why is the derived class version still called? #include struct Base{ void…
CD86
  • 979
  • 10
  • 27
-2
votes
1 answer

Read Access Violation on Virtual Function

This is a simplified version of my program. #include #include class Base { public: Base() { } void virtual update() { std::cout << "no update function\n"; } void virtual draw() { …
-2
votes
1 answer

How can I store address of dynamically created objects in C++?

class A { public: virtual void show() { cout << "you are in A"; } }; class B : public A { public: void show() { cout << "you are in B"; } }; int main() { A *a[5]; A **aa[5]; B *b[5]; for…
-2
votes
1 answer

How to build a operator== in template class

I have a assignment where I'm suppose to build template using these specifications. ISet is a container that holds values ​​of a certain where order doesn't matter and which does not allow duplicates (or multiples). A dynamically allocated array of…
anderssinho
  • 298
  • 2
  • 7
  • 21
-2
votes
1 answer

How to make polymorphic field of class in C++? How to use polymorphism correctly in this case?

I have a class class A { BasePtr * ptr; virtual BasePtr * getPtr() { return ptr; } virtual void pure() = 0; } And I want to derive two classes from it that create its own versions of ptr: class B : public A { B() : A() { ptr = new…
Robotex
  • 1,064
  • 6
  • 17
  • 41
-2
votes
1 answer

Segmentation fault on abstract function call of vector of objects

I am attempting to call a virtual function of a class object. The object is in a 2d vector and I am sure it exists because it prints out the 'symbol,' that I have assigned to it. I have an abstract base class that my other classes derive from.…
-2
votes
3 answers

Can instantiate a virtual object on the heap, but not on the stack?

Let me preface this by saying I'm coming from a Java background, so please forgive me if I've made some stupid mistake here.... I'm writing a C++ library that I would like to make compatible with both Arduino as well as the RaspberryPi. Most of the…
-2
votes
1 answer

Calling virtual method from base

I am trying to make a library for MigraDoc, based on this example, which supports different types of documents. My idea was to make a base class, with a virtual method for CreatePage() (the method responsible for the page layout). However, the…
Jakob Busk Sørensen
  • 5,599
  • 7
  • 44
  • 96
-2
votes
1 answer

How to access virtual functions using vtable?

So I'm trying to access the virtual functions values using vtable. The way I understand it is that the compiler creates a pointer to the vtable, and a vtable is created for each class that contains a virtual function. I was able to get the values…
MrBiscuit
  • 51
  • 1
  • 2
  • 8
-2
votes
1 answer

Is the keyword "virtual" optional or mandatory in a overridable method?

In C #, why we don't have a compilation error even if we forget the keyword "Virtual" in a method of a base class, redefined in a derived class. I learnded that this keyword is mandatory to override a method is not it ? public class Mother { …
-2
votes
2 answers

Linker error with virtual functions c++

I am not sure what is wrong with this code, I am learning Builder pattern. The example code is in Java and I am trying to code the same in C++, butI am getting linker errors. I searched and read about it all and still couldn't find the right way and…
Esash
  • 155
  • 2
  • 13
-2
votes
2 answers

Is it possible to call a virtual function of a derived class without instantiating said class?

I have two classes A and B that derive from an abstract class, Letter. At runtime, I would like to call a function that differs between the derived classes depending on the user input. The following code performs the functionality I…
xyhu617
  • 26
  • 5