Suppose following code is given.
class A
{
public:
virtual void someMethod()
{
std::cout << "class A" << std::endl;
}
};
class B : public A
{
public:
...
virtual void someMethod() = 0;
...
};
Class B
overrides someMethod
virtual method with pure-virtual method.
The purpose of doing this might be extension of existing class which is not allowed to modify in our case class A
, but still having an abstract class B
which has to be base class for some further classes.
According to MISRA-C++ Rule 10-3-3 : Code analyzer gives a warning : Pure virtual function overrides a non pure virtual function .
But I cannot find much details about the warning. What is the side effect of the above mentioned code ? What is the bad practice here ?
UPDATE : the standard is MISRA-C++ (C++98)
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. For example: class Base {
The virtual keyword can be used when declaring overriding functions in a derived class, but it is unnecessary; overrides of virtual functions are always virtual. Virtual functions in a base class must be defined unless they are declared using the pure-specifier.
A pure virtual function (or abstract function) in C++ is a virtual function for which we can have implementation, But we must override that function in the derived class, otherwise the derived class will also become abstract class (For more info about where we provide implementation for such functions refer to this ...
A virtual function is a member function of base class which can be redefined by derived class. A pure virtual function is a member function of base class whose only declaration is provided in base class and should be defined in derived class otherwise derived class also becomes abstract.
I can't see any mystery here. The code analyser is likely checking your code against the MISRA standard, not the C++ 98 standard.
MISRA is a set of C/C++ coding standards for the automotive environment, which imposes further restrictions on what is supposedly legal/permitted by the language standard.
You ARE overriding with a pure virtual function a non pure virtual function, and apparently this is ok with the compiler but not with the MISRA rules.
That is to say, your program will compile and execute fine, and will be compliant with the language standard, but it might not be accepted by a customer that requires code review and compliance with the MISRA standard.
I'd say your code is valid as per standard:
§ 10.4
5 [ 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 ]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With