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

Why do gcc and clang allow me to construct an abstract class?

The following code compiles on a wide range of gcc and clang versions - when compiled and run with gcc 5.3.1, it prints A() then aborts with a pure virtual call error. #include class A { public: A() { printf("A()\n"); …
Matt
  • 253
  • 2
  • 5
24
votes
2 answers

Is there any difference between a private and protected pure virtual function?

I can understand that there might be a reason to declare an implemented (as opposed to pure) virtual function private or protected. Afaik, if you declare an implemented virtual method as protected, your child class can call the base class's method…
Prismatic
  • 3,338
  • 5
  • 36
  • 59
24
votes
8 answers

What are the uses of pure virtual functions in C++?

I'm learning about C++ in a class right now and I don't quite grok pure virtual functions. I understand that they are later outlined in a derived class, but why would you want to declare it as equal to 0 if you are just going to define it in the…
patricksweeney
  • 3,939
  • 7
  • 41
  • 54
22
votes
4 answers

Protect CRTP pattern from stack overflowing in "pure virtual" calls

Consider the following standard CRTP example: #include template struct Base { void f() { static_cast(this)->f(); } void g() { static_cast(this)->g(); } }; struct Foo : public Base { …
uranix
  • 657
  • 1
  • 5
  • 23
22
votes
2 answers

Are Python pure virtual functions possible and/or worth it?

I may be coming from a different mindset, being primarily a C++ programmer. This question has to do with OOP in Python and more specifically pure virtual methods. So taking code I adapted from this question I am looking at this basic sample. class…
benzeno
  • 661
  • 1
  • 6
  • 13
21
votes
6 answers

Pure virtual invocation from constructor and destructor

The C++ standard says that invoking a pure virtual function from a constructor or destructor is forbidden. What is the reason for this? Why should the standard place a restriction like this?
nitin_cherian
  • 6,405
  • 21
  • 76
  • 127
21
votes
4 answers

Why do I have to re-declare overridden functions in derived classes in c++?

Suppose I have the following code: class Iinterface { virtual void abstractFunction()=0; }; class Derived : public Iinterface { void abstractFunction(); // Do I need this line? }; Derived::abstractFunction() { // implementation here } If…
atoMerz
  • 7,534
  • 16
  • 61
  • 101
21
votes
2 answers

cannot declare variable ‘’ to be of abstract type ‘’

EDIT: After spending a bit of time understanding the code I wrote I still don't know what is wrong with it. This is the base class from which I derived my class: ///ContactResultCallback is used to report contact points struct …
SteveDeFacto
  • 1,317
  • 4
  • 19
  • 35
20
votes
4 answers

Why a virtual call to a pure virtual function from a constructor is UB and a call to a non-pure virtual function is allowed by the Standard?

From 10.4 Abstract Classes parag. 6 in the Standard : "Member functions can be called from a constructor (or destructor) of an abstract class; the effect of making a virtual call to a pure virtual function directly or indirectly for the object being…
Belloc
  • 6,318
  • 3
  • 22
  • 52
20
votes
4 answers

What is the purpose of pure virtual destructor?

Possible Duplicates: Under what circumstances is it advantageous to give an implementation of a pure virtual function? Why do we need a pure virtual destructor in C++? Compiler doesn't force the Child class to implement a destructor when its Base…
iammilind
  • 68,093
  • 33
  • 169
  • 336
19
votes
5 answers

The constructor function in a pure virtual class should be "protected" or "public"?

The following example is from the book "Inside C++ object model" class Abstract_base { public: virtual ~Abstract_base () = 0; virtual void interface () const = 0; virtual const char* mumble () const { return _mumble; …
Wizmann
  • 839
  • 1
  • 9
  • 14
18
votes
1 answer

Is it valid to override virtual function with pure specifier?

Note: I do not ask whether or not this is reasonable thing to do or if this is good design. I'm just asking if this is well-defined behaviour and if the results are as expected. I came upon a following class hierarchy: struct A { virtual void…
Yksisarvinen
  • 18,008
  • 2
  • 24
  • 52
16
votes
4 answers

Are there pure virtual functions in PHP like with C++

I would have thought lots of people would have wondered whether this is possible but I can't find any duplicate questions... do correct me. I just want to know whether PHP offers pure virtual functions. I want the following class Parent { // no…
ale
  • 11,636
  • 27
  • 92
  • 149
16
votes
4 answers

Why a pure virtual destructor needs an implementation

I know the cases where pure virtual destructors are needed. I also know that If we don't provide an implementation for them it will give me a linker error. What I don't understand is why this should be the case in a code fragment as shown below: int…
Arun
  • 3,138
  • 4
  • 30
  • 41
14
votes
4 answers

Should an abstract class' destructor be pure virtual?

I think virtual alone is generally sufficient. Is there another reason to make it pure virtual than to force derived classes to implement their own destructor? I mean if you allocate something in your class' constructor you should impement your own…
spc-mrn
  • 257
  • 1
  • 3
  • 11
1 2
3
30 31