pure-specifier on function-definition
Ok, I've just learned something. A pure virtual function must be declared as follows:
class Abstract
{
public:
virtual void pure_virtual() = 0;
};
It may have a body, although it is illegal to include it at the point of declaration. This means that to have a body the pure virtual function must be defined outside the class. Note that even if it has a body, the function must still be overridden by any concrete classes derived from Abstract
. They would just have an option to call Abstract::pure_virtual()
explicitly if they need to.
The details are here.
Pure virtual and inline definition
A pure virtual function may have a definition (out of class definition). That is completely optional. But what you are trying to do is plain wrong because
C++03 [Section 10.4/2
] says:
[Note: a function declaration cannot provide both a pure-specifier and a definition —end note] [Example:
struct C {
virtual void f() = 0 { }; // Ill-formed
}
However you are free to write
struct device{
virtual void switchon() = 0;
};
void device::switchon() { } // Definition {optional}
int main()
{
}
pure virtual destructors in c++ Clion and VS2019
From the C++ 20 (11.6.3 Abstract classes)
- ...[Note: A function declaration cannot provide both a pure-specifier and
a definition — end note] [Example:
struct C {
virtual void f() = 0 { }; // ill-formed
};
— end example]
Pure virtual functions in C++11
Because the syntax says 0
, not expression or some other non-terminal matching nullptr
.
For all the time only 0
has worked. Even 0L
would be ill-formed because it does not match the syntax.
Edit
Clang allows = 0x0
, = 0b0
and = 00
(31.12.2013). That is incorrect and should be fixed in the compiler, of course.
Pure virtual functions may not have an inline definition. Why?
In the SO thread "Why is a pure virtual function initialized by 0?" Jerry Coffin provided this quote from Bjarne Stroustrup’s The Design & Evolution of C++, section §13.2.3, where I've added some emphasis of the part I think is relevant:
The curious
=0
syntax was chosen over the obvious alternative of introducing a new keyword pure or abstract because at the time I saw no chance of getting a new keyword accepted. Had I suggested pure, Release 2.0 would have shipped without abstract classes. Given a choice between a nicer syntax and abstract classes, I chose abstract classes. Rather than risking delay and incurring the certain fights over pure, I used the tradition C and C++ convention of using 0 to represent "not there." The=0
syntax fits with my view that a function body is the initializer for a function and also with the (simplistic, but usually adequate) view of the set of virtual functions being implemented as a vector of function pointers. [ … ]
So, when choosing the syntax Bjarne was thinking of a function body as a kind of initializer part of the declarator, and =0
as an alternate form of initializer, one that indicated “no body” (or in his words, “not there”).
It stands to reason that one cannot both indicate “not there” and have a body – in that conceptual picture.
Or, still in that conceptual picture, having two initializers.
Now, that's as far as my telepathic powers, google-foo and soft-reasoning goes. I surmise that nobody's been Interested Enough™ to formulate a proposal to the committee about having this purely syntactical restriction lifted, and following up with all the work that that entails. Thus it's still that way.
Pure virtual destructor definition inside class gives compilation error
Your second example is correct.
A lot of the other answers assume that it is illegal to have a pure virtual function with a default implementation, however that is incorrect.
In the case of a pure virtual destructor you must have a definition (see the link in xmoex answer).
It is true that:
§10.4/2 a function declaration cannot provide both a pure-specifier
and a definition
However, as you noticed it possible to provide a definition outside of the declaration.
Is it valid to override virtual function with pure specifier?
[class.abstract/5] of the current draft Standard:
[Note: An abstract class can be derived from a class that is not abstract, and a pure virtual function may override a virtual function which is not pure. — end note]
The very same note is included even in the C++11 Standard. So, the answer is yes, it is valid.
Invalid Pure Specifier
The problem is not with the functions here. There is some macro somewhere (#define delete _leaker_file=FILE, _leaker_func=func
) which breaks stuff.
The error comes from leaker.h:95:28
(leaker.h
, line 95, column 28). I guess you have a deleted function there:
leaker(const leaker&) = delete;
Will be translated to:
leaker(const leaker&) = _leaker_file=FILE, _leaker_func=func;
And compiler has no idea how to proceed.
If this is your macro, remove it immidiately from code. It's always going to bring you woe and misery.
If it is imported from any other file, don't #include
that file and you should really reconsider using that library - if it uses such mechanisms, it cannot be a good library - it's Undefined Behaviour to override keywords with macros.
If you cannot find this macro, try commenting out every #include
from your header, until the problem goes away. You can comment out your current class and make some dummy class to focus on the problem:
//#include <vector>
#include <...>
class A {
A() = delete;
};
//class leaker {
//...
//};
Pure Virtual Destructor with Default Keyword
No.
You will have to write a separate definition and default
it there, as you've shown.
The presence of a pure-specifier precludes the presence of a definition at the same location, even when that definition is just a = default
.
Related Topics
Does It Make Sense for Unary Operators to Be Associative
C++ 2 Dimensional Array with Variable Size Rows
C++: Rotating a Vector Around a Certain Point
Problem with Compiling Rinside Examples Under Windows
C++ Uniform_Int_Distribution Always Returning Min() on First Invocation
Visual Studio 2015 Code Analysis C6386 Warns of Buffer Overrun
How to Vertically Align Text in Edit Box
Get Key Press in Windows Console
Redirect Both Cout and Stdout to a String in C++ for Unit Testing
What Are the Advantages and Disadvantages of Implementing Classes in Header Files
What Are Practical Applications of Weak Linking
C++ "Hello World" Boost Tee Example Program
How to Determine If Returned Pointer Is on the Stack or Heap
Dealing with Floating Point Exceptions
Can't Access Derived Class Method from Pointer of Type Base Class