Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why can't I declare a pure virtual function with `= delete;`?

Tags:

c++

c++11

Intro

Pure virtual functions are delcared with the common syntax :

virtual f() = 0; 

Yet, since c++11 there's a way to communicate the explicit non existence of a (special) member function :

Mystruct() = delete; // eg default constructor

Q

Why isn't this syntax extended to pure virtual functions in order to achieve uniformity in communicating such operations ? :

virtual f() = delete; 

Note

I know the obvious answer is because the Standard says so!. I'm wondering about the reason(ing) behind this and whether there ever was a proposal (or an intention) for something like this.

like image 545
Nikos Athanasiou Avatar asked Jul 25 '14 14:07

Nikos Athanasiou


People also ask

What is the correct way to declare a pure virtual function?

You declare a pure virtual function by using a pure specifier ( = 0 ) in the declaration of a virtual member function in the class declaration. Class A is an abstract class. The compiler would not allow the function declarations A g() or void h(A) , declaration of object a , nor the static cast of b to type A .

What is the correct way to declare a pure virtual function in C++? Pick one option?

Q) Which is the correct declaration of pure virtual function in C++ virtual void func() = 0; is the correct declaration.

Do pure virtual functions have to be overridden?

¶ Δ A pure virtual function is a function that must be overridden in a derived class and need not be defined. A virtual function is declared to be “pure” using the curious =0 syntax.

Which option is used to create a pure virtual function?

2. Which is used to create a pure virtual function? d) ! Explanation: For making a method as pure virtual function, We have to append '=0' to the class or method.


1 Answers

Roughly speaking, the difference is that this:

virtual void f() = 0; 

says "This class is abstract, and I may not have written an implementation of this member function" (though you are allowed to).

However, this:

void f() = delete;

says "This member function literally does not exist, and none shall pass."

like image 146
Lightness Races in Orbit Avatar answered Nov 24 '22 06:11

Lightness Races in Orbit