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
-6
votes
1 answer

Upcast, downcasting, and hiding methods in C++

I'm writing a C++ library which needs to creates a complex object (D), but return to the user of my library a pointer to a simplified object (B) (hiding many of the methods of the complex object). I have decided to implement this by returning a…
TSG
  • 4,242
  • 9
  • 61
  • 121
1 2 3
30
31