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
14
votes
5 answers

C++ template duck-typing vs pure virtual base class inheritance

Which are the guidelines for choosing between template duck-typing and pure virtual base class inheritance? Examples: // templates class duck { void sing() { std::cout << "quack\n"; } }; template void somefunc(const bird& b) { …
Giovanni Funchal
  • 8,934
  • 13
  • 61
  • 110
14
votes
2 answers

C++ 11 Delegated Constructor Pure Virtual Method & Function Calls -- Dangers?

Not a Duplicate of Invoking virtual function and pure-virtual function from a constructor: Former Question relates to C++ 03, not new Constructor Delegation behavior in C++ 11, and the question does not address the mitigation of undefined behavior…
e.s. kohen
  • 213
  • 1
  • 4
  • 21
13
votes
2 answers

Any way to make Eclipse auto implement pure virtual functions?

I declare some class with pure virtual methods and make some classes derive it. Is there any way in Eclipse CDT to locate the pure virtual functions of the parent class and auto complete the declaration of the children class, just like in the java…
ibrabeicker
  • 1,786
  • 2
  • 19
  • 31
13
votes
5 answers

Pure virtual destructor definition inside class gives compilation error

The pure virtual destructor in base class should have a definition. Otherwise compiler will generate a call to base class destructor from the derived class destructor during link-time and will cause a link-error. I tried to define the pure virtual…
nitin_cherian
  • 6,405
  • 21
  • 76
  • 127
13
votes
4 answers

Does it make any sense to define "pure" virtual functions in the base class itself?

The benefit of defining common virtual functions in the base class is that we don't have to redefine them in the derived classes then. Even if we define pure virtual functions in the base class itself, we'll still have to define them in the…
Aquarius_Girl
  • 21,790
  • 65
  • 230
  • 411
13
votes
3 answers

C++'s pure virtual function implementation and header files

I'm having some trouble implementing pure virtual functions inherited from some abstract class, when the classes in question are divided into *.h and *.cpp files. The compiler (g++) tells me that the derived class cannot be instantiated because of…
Neo
  • 1,176
  • 2
  • 10
  • 15
13
votes
7 answers

Deriving an abstract class from concrete class

Let's say we have a concrete class Apple. (Apple objects can be instantiated.) Now, someone comes and derives an abstract class Peach from Apple. It's abstract because it introduces a new pure virtual function. The user of Peach is now forced to…
Marcin
  • 12,245
  • 9
  • 42
  • 49
13
votes
4 answers

Do GCC's function __attribute__s work with virtual functions?

The GCC C++ compiler offers a family of extensions via function attributes, such as: int square(int) __attribute__((const)); Two attributes in particular, const and pure, allow you to declare that a function's evaluation has no side effects and…
Kerrek SB
  • 464,522
  • 92
  • 875
  • 1,084
12
votes
1 answer

How do I denote a pure virtual function in a UML class diagram?

I am a student learning C++. I am creating a UML class diagram for my program that involves inheritance and abstract / concrete classes, but I'm not too sure how I would denote a pure virtual function. Any help is appreciated, thank you!
john grey
  • 123
  • 1
  • 1
  • 4
12
votes
3 answers

Move semantics and virtual methods

In C++11 we are guided in some cases to pass objects by value and in others by const-reference. However, this guideline depends on the implementation of the method, not just on its interface and intended usage by its clients. When I write an…
Alex Shtoff
  • 2,520
  • 1
  • 25
  • 53
12
votes
3 answers

Pure virtual operator

I have a project for school in C++ and I am stuck on one part: I have to overload the operators + and * to work with geometrical figures. That was no problem, but here it where it doesn’t work: I have to declare the operator as a pure virtual…
Casandra
  • 123
  • 1
  • 5
11
votes
2 answers

Is it dangerous to create pure virtual function of a virtual function?

Let us suppose that we have an abstract class NonEditableSuperBase from which we create another abstract class MyBase. The first class NonEditableSuperBase has a virtual function (non pure virtual). However, I want to force that if someone creates a…
pablo_worker
  • 1,042
  • 9
  • 26
11
votes
1 answer

How to implement an abstract method when abstract class is used in a variadic context

How to implement in the following code the abstract base class in a generic case. The code is simplified from a library I am working on. So an explicit implementation for int and double is not an option. template struct Foo { virtual…
11
votes
2 answers

Why is this not a call of a pure virtual function?

I tried to "repair" the example in this answer as to demonstrate how a pure virtual function can be called. #include using namespace std; class A { int id; public: A(int i): id(i) {} int callFoo() { return foo(); } …
Wolf
  • 9,679
  • 7
  • 62
  • 108
10
votes
7 answers

Pure Virtual Method Called

EDIT: SOLVED I'm working on a multi-threaded project right now where I have a base worker class, with varying worker classes that inherit from it. At runtime, the worker classes become threads, which then perform work as needed. Now, I have a…
jakogut
  • 4,409
  • 6
  • 29
  • 41