Questions tagged [pure-virtual]

A virtual function that must be implemented by every non-abstract derived class. Typically, this is used when the progammer wants to guarantee that a function will exist at run-time but where there are multiple ways of defining its behaviour with no obvious "best way".

Though the term pure-virtual is generally only used in C++, analogs exist in other languages. In Java, for example, an abstract method (declared with the abstract keyword) fulfills the same purpose. The primary reason for this shift in terminology is that in Java all methods are virtual (i.e. will execute the definition closest to the true run-time type of the object, rather than the definition closest to the type being referenced).

A pure-virtual method however specifies that definition will be deferred until the proper definition of a type, forcing polymorphic behavior by not specifying a default implementation for derived types to fall back on.

In Java and similar languages, abstract methods require that the class is also declared as abstract. In C++, the existence of a pure virtual method prevents instantiation, without the need for the programmer to explicitly mark the class as abstract.

451 questions
883
votes
17 answers

How do you declare an interface in C++?

How do I setup a class that represents an interface? Is this just an abstract base class?
Aaron Fischer
  • 20,853
  • 18
  • 75
  • 116
242
votes
4 answers

Difference between a virtual function and a pure virtual function

What is the difference between a pure virtual function and a virtual function? I know "Pure Virtual Function is a Virtual function with no body", but what does this mean and what is actually done by the line below: virtual void virtualfunctioname()…
Sachin
  • 20,805
  • 32
  • 86
  • 99
216
votes
10 answers

Pure virtual function with implementation

My basic understanding is that there is no implementation for a pure virtual function, however, I was told there might be implementation for pure virtual function. class A { public: virtual void f() = 0; }; void A::f() { …
skydoor
  • 25,218
  • 52
  • 147
  • 201
190
votes
11 answers

Why do we need a pure virtual destructor in C++?

I understand the need for a virtual destructor. But why do we need a pure virtual destructor? In one of the C++ articles, the author has mentioned that we use pure virtual destructor when we want to make a class abstract. But we can make a class…
Mark
  • 2,035
  • 3
  • 14
  • 7
173
votes
2 answers

Pure virtual destructor in C++

Is it wrong to write: class A { public: virtual ~A() = 0; }; for an abstract base class? At least that compiles in MSVC... Will it crash at run time?
Ivan Krechetov
  • 18,802
  • 8
  • 49
  • 60
167
votes
11 answers

Why is a pure virtual function initialized by 0?

We always declare a pure virtual function as: virtual void fun () = 0 ; I.e., it is always assigned to 0. What I understand is that this is to initialize the vtable entry for this function to NULL and any other value here results in a compile time…
mukeshkumar
  • 2,698
  • 3
  • 19
  • 20
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
82
votes
6 answers

Benefits of pure function

Today i was reading about pure function, got confused with its use: A function is said to be pure if it returns same set of values for same set of inputs and does not have any observable side effects. e.g. strlen() is a pure function while rand()…
Green goblin
  • 9,898
  • 13
  • 71
  • 100
74
votes
3 answers

What does it mean to set the declaration of a function equal to 0? How can you assign an integer to a function?

I was browsing through the sources of a (prefer not to name) GUI Toolkit which wrapped up the Windows API when I found the following function definition in the window class: virtual LRESULT CALLBACK wndProc (HWND, UINT, WPARAM, LPARAM) = 0; What…
ApprenticeHacker
  • 21,351
  • 27
  • 103
  • 153
71
votes
3 answers

C++ pure virtual function have body

Pure virtual functions (when we set = 0) can also have a function body. What is the use to provide a function body for pure virtual functions, if they are not going to be called at all?
Vijay
  • 2,021
  • 4
  • 24
  • 33
66
votes
2 answers

What is the purpose of __cxa_pure_virtual?

Whilst compiling with avr-gcc I have encountered linker errors such as the following: undefined reference to `__cxa_pure_virtual' I've found this document which states: The __cxa_pure_virtual function is an error handler that is invoked when a…
Matthew Murdoch
  • 30,874
  • 30
  • 96
  • 127
65
votes
2 answers

Error: expected type-specifier before 'ClassName'

shared_ptr circle(new Circle(Vec2f(0, 0), 0.1, Vec3f(1, 0, 0))); shared_ptr rect(new Rect2f(Vec2f(0, 0), 5.0f, 5.0f, 0, Vec3f(1.0f, 1.0f, 0)) ); I'm trying to understand why the above…
zeboidlund
  • 9,731
  • 31
  • 118
  • 180
65
votes
5 answers

Pure virtual functions may not have an inline definition. Why?

Pure virtual functions are those member functions that are virtual and have the pure-specifier ( = 0; ) Clause 10.4 paragraph 2 of C++03 tells us what an abstract class is and, as a side note, the following: [Note: a function declaration cannot…
Armen Tsirunyan
  • 130,161
  • 59
  • 324
  • 434
60
votes
4 answers

Is there any point in using `override` when overriding a pure virtual function?

For example: class Base { virtual void my_function() = 0; }; class Derived : Base { void my_function() override; }; From what I read, the override keyword is used to make sure that we have the correct signature in the function that we are…
R2B2
  • 1,541
  • 1
  • 12
  • 19
56
votes
5 answers

Undefined symbols "vtable for ..." and "typeinfo for..."?

Nearly the final step but still some strange erros.... bash-3.2$ make g++ -Wall -c -g Myworld.cc g++ -Wall -g solvePlanningProblem.o Position.o AStarNode.o PRM.o PRMNode.o World.o SingleCircleWorld.o Myworld.o RECTANGLE.o CIRCLE.o -o…
Lisa
  • 571
  • 3
  • 7
  • 10
1
2 3
30 31