简体   繁体   English

“使用”未解决“隐藏虚拟功能”警告

[英]“Using” is not solving the “hides virtual function” warning

I have been googling high and low and can't find a solution that will remove the warning, even when I use the using directive. 我一直在搜寻谷歌,即使我使用using指令,也找不到能消除警告的解决方案。

class TShowException_Form : public TForm {

    __published: // IDE-managed Components
        TButton *Send_Button;
        TButton *Cancel_Button;
        TLabel  *Message_Label;

    private:    // User declarations
        using TCustomForm::ShowModal;
        //using TForm::ShowModal;

    public:     // User declarations
            __fastcall TShowException_Form(TComponent* Owner);
        int __fastcall ShowModal(System::Sysutils::Exception *E);
};

I want to hide the original virtual int __fastcall ShowModal(void) and expose a new one taking an Exception parameter. 我想隐藏原始的virtual int __fastcall ShowModal(void)并公开一个带有Exception参数的新virtual int __fastcall ShowModal(void)

But it still complaints on "hides virtual function": 但是它仍然抱怨“隐藏虚拟功能”:

[bcc32 Warning] TShowExceptionForm.h(32): '_fastcall TShowException_Form::ShowModal(Exception *)' hides virtual function '_fastcall TCustomForm::ShowModal()'

I have also tried using TForm::ShowModal; 我也尝试过using TForm::ShowModal; but with the same result. 但结果相同。 Any ideas of how to solve this warning? 关于如何解决此警告的任何想法?


EDIT 编辑
I found out that it works perfectly well if I override the show() method instead: 我发现,如果我改用show()方法,则效果很好:

class TShowException_Form : public TForm {

    __published: // IDE-managed Components
        TButton *Send_Button;
        TButton *Cancel_Button;
        TLabel  *Message_Label;

    private:    // User declarations
        using TForm::ShowModal;
        using TForm::Show;

    public:     // User declarations
            __fastcall TShowException_Form(TComponent* Owner);
        int __fastcall Show(System::Sysutils::Exception *E);
};

So why isn't it working with ShowModal() ? 那么为什么它不能与ShowModal()呢?

You must declare overloaded version as virtual too: 您还必须将重载版本声明为virtual版本:

virtual int __fastcall ShowModal(System::SysUtils::Exception * E);

Does not know if C++Builder supports C++11, but if it does, try also to delete overload which you want to hide: 不知道C ++ Builder是否支持C ++ 11,但是如果支持,还尝试delete要隐藏的重载:

virtual int __fastcall ShowModal() = delete;

instead of placing it into private section. 而不是将其放在专用部分。

bcc32 is, in many respects, not very compliant with the C++ standard. 在许多方面,bcc32与C ++标准不太兼容。 Whenever I find myself asking, "Why does this technique that I think should work in C++ not work in bcc32?", I usually assume it's yet another compiler bug. 每当我问自己“为什么我认为应该在C ++中使用的这项技术为什么不能在bcc32中使用?”时,我通常会认为这是另一个编译器错误。

The fact that Show works while ShowModal doesn't is interesting. Show有效而ShowModal无效的事实很有趣。 Looking at Vcl.Forms.hpp shows the difference: Show is defined with HIDESBASE (a macro that expands to __declspec(hidesbase) ). 查看Vcl.Forms.hpp可以看到以下区别: Show是使用HIDESBASE定义的(扩展为__declspec(hidesbase)的宏)。

Adding HIDESBASE to your ShowModal should work as well. 将HIDESBASE添加到ShowModal应该也可以正常工作。 You may also have to declare a virtual destructor, if you don't already have one, due to bcc32 compiler weirdness. 由于bcc32编译器很怪异,您可能还必须声明一个虚拟析构函数(如果还没有的话)。

virtual __Fastcall ~TShowException_Form() {}

You get the warning because what you are trying to do happens very often by mistake and is a serious and very hard to find bug if it is done by mistake. 之所以会发出警告,是因为您尝试做的事情经常会错误地发生,并且如果错误地做的话,这是一个严重且很难发现的错误。 Maybe you should use a different name. 也许您应该使用其他名称。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM