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

查看:1434
本文介绍了在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:

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

    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天全站免登陆