Pure virtual function overridding virtual function
Asked Answered
M

3

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)

Metcalf answered 28/9, 2013 at 10:20 Comment(8)
Is this intended to be a poor mans version of C++11's virt-specifier final ? (C++11 10.3p4)Mucus
If that gives a warning, then what's std::is_abstract for?Equality
please check the updated question, the standard is c++98Metcalf
possible duplicate of Deriving an abstract class from concrete classEquality
ok, let me review, but I need some time, paper about Liskov's substitutability is quit large.Metcalf
LSP is actually quite simple. It says that the behaviour should remain the same regardless what kind of class you use.Pixie
To fix the warnings: you may use something like: class B : public A { public: virtual void someMethod() {someMethodB();} virtual void someMethodB() = 0; };Muliebrity
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]Antiphony
C
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.

Comeback answered 28/9, 2013 at 10:45 Comment(1)
Yes, your are absolutely right, I mentiond C++98, as C++11 related features were pointed to.Metcalf
L
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 ]

Lohrman answered 28/9, 2013 at 10:33 Comment(4)
it is not the compiler that warns falsely. but it is a MISRA rulePixie
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.Muliebrity
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.Mucus
@Muliebrity 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 ?Metcalf
G
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;
   }
};
Gnarly answered 9/4, 2022 at 1:21 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.