在 C++/Qt 中定义纯虚拟信号是否有效? [英] Is it valid to define a pure virtual signal in C++/Qt?

查看:45
本文介绍了在 C++/Qt 中定义纯虚拟信号是否有效?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作一个抽象基类,并认为我可能想要一个纯虚拟信号.但是当我编译时,我收到了我定义的纯虚拟信号的警告:

I am making an abstract-base-class and was thinking I might want a pure virtual signal. But when I compiled I get a warning for the pure virtual signals I have defined:

../FILE1.h:27: Warning: Signals cannot be declared virtual
../FILE1.h:28: Warning: Signals cannot be declared virtual

在C++/Qt中定义纯虚拟信号有效吗?定义虚拟信号有效吗?

Qt 的信号和插槽文档页面 说你可以定义虚拟插槽但不谈论信号.我似乎找不到关于纯虚拟信号的好信息.

Qt's signal and slot documentation page says you can define virtual slots but doesn't talk about signals. I can't seem to find good information on pure virtual signals.

推荐答案

  • 信号从来没有实现[1](即您在 .h 文件中定义信号,然后在 .cpp 中没有实现).
  • 声明函数纯虚拟的主要目的是强制继承类提供实现.
  • 鉴于以上两个陈述,这是我的想法:

    Given the above two statements here's my thinking:

    信号没有实现,但将其声明为纯虚拟将需要继承类提供实现……这与信号没有实现"直接冲突.这就像让某人同时出现在两个地方一样,这是不可能的.

    Signals don't have an implementation but declaring it pure virtual will require the inheriting class to provide an implementation... which directly conflict with "signals don't have an implementation". It's like asking someone to be in two places at once it's just not possible.

    所以总而言之,声明纯虚拟"信号"似乎是错误的,因此无效.

    在抽象基类的情况下,我认为这是正确的:

    In the case of an abstract base class here's what I think is correct:

    当您声明该函数仅为虚拟"时,它仍会发出警告.为了避免任何警告,我认为解决方案是不使用任何虚拟"或纯虚拟"限定信号,然后继承类将不会声明任何信号,但仍然可以发出基类中定义的信号.

    When one declares the function only "virtual" it still gives the warning. To avoid any warnings I think the solution is to not qualify the signal with any "virtual" or "pure virtual" and then the inheriting class will not declare any signals but can still emit the signals defined in the base class.

    [1] 当我说信号永远没有实现"时,我的意思是实现类的人不提供实现.我知道在幕后 Qt 的 moc 在 moc_FILE1.cpp 中提供了一个实现.

    [1] when I say that "signals don't ever have an implementation" I mean that the person implementing the class doesn't provide the implementation. I understand that behind the scene Qt's moc provides an implementation in the moc_FILE1.cpp .

    这篇关于在 C++/Qt 中定义纯虚拟信号是否有效?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆