From the standard (9.2 Class members [class.mem]):
= 0
is the pure-specifier
It tells that compiler that:
- the class is abstract
- the method will be defined outside the class definition
(usually in a derived class)
Example 1 (build fails)
If I understand your question correctly, you have something like that:
class MyClass {
public:
virtual void X() = 0;
};
class MyDerivedClass : MyClass {
public:
virtual void X();
};
void MyDerivedClass::X() { MyClass::X(); }
int main()
{
MyDerivedClass mdc;
mdc.X();
return 0;
}
If so, the build should fail with:
Error:
undefined reference to 'MyClass::X()'
Example 2 (build succeeds)
However, even if the method MyClass::X()
is declared as pure virtual,
you can provide a definition. The following would work. The class MyClass
is still abstract, but you can call the method MyClass::X()
.
#include <iostream>
class MyClass {
public:
virtual void X() = 0; // pure virtual method
};
class MyDerivedClass : MyClass {
public:
virtual void X();
};
void MyClass::X() { // pure virtual method definition
std::cout << "MyClass::X()" << std::endl;
}
void MyDerivedClass::X() {
MyClass::X();
std::cout << "MyDerivedClass::X()" << std::endl;
}
int main()
{
MyDerivedClass mdc;
mdc.X();
return 0;
}
Output:
MyClass::X()
MyDerivedClass::X()