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
54
votes
8 answers

call to pure virtual function from base class constructor

I have a base class MyBase that contains a pure virtual function: void PrintStartMessage() = 0 I want each derived class to call it in their constructor then I put it in base class(MyBase) constructor class MyBase { public: virtual void…
50
votes
7 answers

Pure virtual methods in C#?

I've been told to make my class abstract: public abstract class Airplane_Abstract And to make a method called move virtual public virtual void Move() { //use the property to ensure that there is a valid position object …
David Brewer
  • 1,864
  • 8
  • 25
  • 37
41
votes
6 answers

Implement a pure virtual method in Objective-C

I want to go to there. Seriously though, how does one implement a pure virtual method in an "Apple" way? Do you use a Protocol with your base class and throw exceptions on those methods?
Chris
  • 645
  • 1
  • 5
  • 11
41
votes
8 answers

Pure virtual functions in C++11

In C++98, the null pointer was represented by the literal 0 (or in fact any constant expression whose value was zero). In C++11, we prefer nullptr instead. But this doesn't work for pure virtual functions: struct X { virtual void foo() =…
fredoverflow
  • 256,549
  • 94
  • 388
  • 662
40
votes
6 answers

pure-specifier on function-definition

While compiling on GCC I get the error: pure-specifier on function-definition, but not when I compile the same code using VS2005. class Dummy { //error: pure-specifier on function-definition, VS2005 compiles virtual void Process() = 0…
Bébul
  • 575
  • 1
  • 5
  • 7
34
votes
2 answers

Is there a pure virtual function in the C++ Standard Library?

In Nicola Gigante's lecture in 2015, he mentions (at the beginning) that there are no pure virtual functions in the Standard Library (or he's not aware of any). I believe that Alex Stepanov was against this language feature but since the initial STL…
33
votes
2 answers

"Cannot allocate an object of abstract type" error

Error is here: vector graduates; graduates.push_back(new AliceUniversity(identifier,id,salary,average)); Grandparent class: Graduate::Graduate(char identifier, long id, int salary, …
Itzik984
  • 15,968
  • 28
  • 69
  • 107
33
votes
4 answers

What can cause a pure virtual function call in C++?

I teach a C++ programming class and I've seen enough classes of errors that I have a good feeling for how to diagnose common C++ bugs. However, there's one major type of error for which my intuition isn't particularly good: what programming errors…
templatetypedef
  • 362,284
  • 104
  • 897
  • 1,065
31
votes
3 answers

C++ abstract class without pure virtual functions?

I have a base class class ShapeF { public: ShapeF(); virtual ~ShapeF(); inline void SetPosition(const Vector2& inPosition) { mPosition.Set(inPosition); } protected: Vector2 mPosition; } Obviously with some ommitied code, but you…
MintyAnt
  • 2,978
  • 7
  • 25
  • 37
31
votes
3 answers

How to resolve "pure virtual method called"

I understand why this is happening, but I'm stuck trying to resolve it...here is what my code is doing when the error is generated (thus, leading to a crash) when my program exits... pure virtual method called SomeClass::~SomeClass() { …
user869525
  • 769
  • 2
  • 12
  • 21
29
votes
2 answers

Can I override a virtual function with a pure virtual one?

I have three classes: B, D and G. D is a B and G is a D. Both B and D are abstract. B is from a third party. B has a non-pure, virtual method that G needs to implement (to be a D). Can I and is it good practice to redefine/override a virtual…
jtooker
  • 1,043
  • 1
  • 8
  • 22
28
votes
6 answers

Under what circumstances is it advantageous to give an implementation of a pure virtual function?

In C++, it is legal to give an implementation of a pure virtual function: class C { public: virtual int f() = 0; }; int C::f() { return 0; } Why would you ever want to do this? Related question: The C++ faq lite contains an example: class…
Tobias
  • 6,388
  • 4
  • 39
  • 64
28
votes
2 answers

C++ override pure virtual method with pure virtual method

Does it ever make sense to override a pure virtual method with another pure virtual method? Are there any functional differences or perhaps code style reasons to prefer one of the following options over the other? class Interface { public: …
AffluentOwl
  • 3,337
  • 5
  • 22
  • 32
26
votes
4 answers

Doesn't putting a "virtual destructor inside an interface" make it, by definition, not an interface anymore?

So here is the box I am in. I want to understand why it is important to have a "virtual destructor inside your interface class". You will see why that stuff is in quotes if you can hang to the end... I also want to get all the vocabulary…
Jimmy
  • 4,419
  • 6
  • 21
  • 30
26
votes
3 answers

pure virtual final functions : legal in C++11

class Foo { public: virtual int foo() final = 0; }; Compiles fine. Isn't Foo just a waste of space, and an accident in the making? Or am I missing something?
aiao
  • 4,621
  • 3
  • 25
  • 47
1
2
3
30 31