Incorrect warning about "Inherited functions should not be hidden"

Version used: SonarQube Enterprise Edition Version 8.9 (build 43852)
#bug:fp cpp:S1242

I have observed a bug with the following type of code in C++.

class ArgumentClass {
public:
...
};
class Interface {
public:
virtual void func(const ::std::vector<const ArgumentClass*>& args) const = 0;
virtual void func(const ::std::vector<ArgumentClass*>& args) const = 0;
};
class ImplClass : public virtual Interface {
public:
void func(const ::std::vector<const ArgumentClass*>& args) const override;
void func(const ::std::vector<ArgumentClass*>& args) const final;
};
class SubClass : public virtual ImplClass {
public:
void func(const ::std::vector<const ArgumentClass*>& args) const override;
};

I get the following warning:
Correct these functions so that no function in "SubClass::func" hides a function in "ImplClass::func"

Here’s the problem. I get an error message when I pass a vector of non-const objects to a function expecting const objects. So I have a function whose sole purpose is to convert the argument type to make the class useable with const and non-const arguments. That’s why I have both functions in the interface. The common implementation class (ImplClass) just converts the non-const objects to the const ones, and it’s marked final. In the SubClass, I have it override the behavior of just the one using the const objects.

I shouldn’t be getting this warning when the conflicting one is marked final. There is no ambiguity here, and there is no hiding. I think this warning should only kick in with regards to non-final functions.

This is a helpful warning in other circumstances, but not in this one. How can this issue get addressed?

Thank you.

Hello @BlackDiamond and welcome to our community!

You’re right indeed, you should not receive a warning in this case. Thanks for pointing it out! I created a ticket (see CPP-3129) to fix that.
In the meantime, you can mark this issue as “False positive” in SonarQube, through the issues interface to get rid of it.

Cheers,
Amélie

Thanks! I also get the same issue when the function has the same name but with a different number of parameters, and I’m only overriding one of them. Neither of them are final. It’s following a Java style of parameter defaulting.

Hello @BlackDiamond,

Without actual code, it’s difficult to be sure… But the way I understood what you describe, this is actually a case of hiding functions. For instance, the following example will fail to compile, because of the function hiding:

struct Base{
    virtual void f(int i) {}
    virtual void f(int i, int j) {}
};

struct Derived : public Base {
    void f(int i, int j) override {}
};

void f() {
    Derived d;
    d.f(2);
}

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.