Home > Software engineering >  solve clang-12 overloaded-virtual warning
solve clang-12 overloaded-virtual warning

Time:03-02

The code below gives a clang-12 warning:

warning: 'foo::TIFFFormat::encodePixels' hides overloaded virtual function [-Woverloaded-virtual]

What can I do to solve the problem described by this warning ?

namespace foo {

struct bar {
    int k;
};

class IImageFormat
{
  public:
    virtual ~IImageFormat() = default;
    virtual bool encodePixels(void) = 0;
    virtual bool encodePixels(bar pixels) = 0;
};



class ImageFormat : public IImageFormat
{
  public:
    bool encodePixels(bar pixels) override;

};


bool ImageFormat::encodePixels(bar pixels){
    (void)pixels;
    return false;
}


class TIFFFormat : public ImageFormat
{
  public:
    bool encodePixels() override;
};

bool TIFFFormat::encodePixels(){
    return false;
}

}


foo::TIFFFormat tf;

CodePudding user response:

Declaring one of the two overloads in the derived class, but not the other, will cause name lookup from the derived class to find only the one declared in the derived class.

So with your code you will not be able to call e.g. tf.encodePixels(foo::bar{}).

If you don't want to repeat all overloads in the derived class, you can import all of them via a using declaration in TIFFFormat:

using ImageFormat::encodePixels;

If you don't care or intent the overload to not be reachable from the derived class, then the warning is not relevant to you.

  • Related