8

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)

deimus
  • 9,565
  • 12
  • 63
  • 107
  • Is this intended to be a poor mans version of C++11's virt-specifier `final` ? (C++11 10.3p4) – WhozCraig Sep 28 '13 at 10:33
  • If that gives a warning, then what's [std::is_abstract](http://en.cppreference.com/w/cpp/types/is_abstract) for? – raina77ow Sep 28 '13 at 10:34
  • please check the updated question, the standard is c++98 – deimus Sep 28 '13 at 10:37
  • possible duplicate of [Deriving an abstract class from concrete class](http://stackoverflow.com/questions/310408/deriving-an-abstract-class-from-concrete-class) – raina77ow Sep 28 '13 at 10:37
  • ok, let me review, but I need some time, paper about Liskov's substitutability is quit large. – deimus Sep 28 '13 at 10:44
  • LSP is actually quite simple. It says that the behaviour should remain the same regardless what kind of class you use. – BЈовић Sep 28 '13 at 10:45
  • To fix the warnings: you may use something like: `class B : public A { public: virtual void someMethod() {someMethodB();} virtual void someMethodB() = 0; };` – Jarod42 Sep 28 '13 at 11:18
  • A reminder that the process of MISRA Compliance requires that the programmer has the required *competence* - this is also a requirement of ISO 9001 and all of the functional safety standards. MISRA C/C++ is not just about running a tool over your source code. This question suggests that the OP is not familiar with, nor has access to, MISRA C++ ... which in turn raises alarm bells at the organisational controls in place! [See profile for affiliation] – Andrew Apr 09 '22 at 19:27

3 Answers3

6

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.

vinaut
  • 2,416
  • 15
  • 13
  • Yes, your are absolutely right, I mentiond C++98, as C++11 related features were pointed to. – deimus Sep 28 '13 at 10:48
3

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 ]

billz
  • 44,644
  • 9
  • 83
  • 100
  • 1
    it is not the compiler that warns falsely. but it is a MISRA rule – BЈовић Sep 28 '13 at 10:42
  • It is not a false reports. MISHA considers that as a _bad practice_. (Why not declare A::SomeMethod() as virtual pure ?). It may be considered abusive as some others MISHA rules. – Jarod42 Sep 28 '13 at 10:48
  • Side Note: This is *identical* in both C++98 and C++11. Is see no difference between the two, including the cited section and paragraph notes. – WhozCraig Sep 28 '13 at 10:49
  • @Jarod42 as I mentioned in question, I dont' have an access of modifying class A, But still need an abstract class B, probably its better to add some other dummy abstract method, and not touch the someMethod ? – deimus Sep 28 '13 at 10:53
0

The inheritance is backwards.

It has class A inherited by class B. B has the pure virtual function. I believe that you want the following code. It says the child classes of B must implement somemethod().

class B
{
public:
   ...
   virtual void someMethod() = 0;
   ...
};

class A : public B
{
public:
   virtual void someMethod()
   {
      std::cout << "class A" << std::endl;
   }
};
GlenL
  • 39
  • 7