C++:为事件处理程序创建一个匿名类

C++: Create an anonymous class for event handlers(C++:为事件处理程序创建一个匿名类)

本文介绍了C++:为事件处理程序创建一个匿名类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

免责声明:此描述包含许多 Qt 细节.他们不需要回答这个问题,我只是想给你背景.

Disclaimer: This description contains a lot of Qt specifics. They are not necessary to answer the question, I just wanted to give you the background.

我需要对 QTextEditfocusInEvent 做出反应.不幸的是,这不能作为信号使用,这就是为什么我需要继承 QTextEdit.由于这是我需要的唯一更改,我想使用匿名子类

I need to react on the focusInEvent of a QTextEdit. Unfortunately this is not available as a signal, that's why I need to subclass QTextEdit. Since this is the only change I need, I would like to use an anonymous subclass

像这样:

myTextEdit =new QTextEdit(){
            void focusInEvent(){
     //code here
     }
};

这是我用 Java 编写的代码,它不能用 c++ 编译.以下所有代码都在自定义 QWidget 的构造函数中.QTextEdit 包含在此小部件中,应在其构造函数中初始化.

This is the code I would write in Java, it doesn't compile in c++. All following code is within the constructor of a custom QWidget. The QTextEdit is contained in this widget and should be initialized in its constructor.

奇怪的是这段代码编译:

Strangely this code compiles:

class MyTextEdit:protected QTextEdit{
    void focusInEvent();
};
auto myTextEdit=new MyTextEdit();

但没用,因为我无法将 myTextEdit* 的实例分配给指向 QTextEdit 的指针.不知何故,多态性失败了.此代码无法编译:

but is useless, since I can't assign an instance of myTextEdit* to a pointer to QTextEdit. Somehow polymorphism fails. This code doesn't compile:

class MyTextEdit:protected QTextEdit{
        void focusInEvent();
    };
QTextEdit* myTextEdit=new MyTextEdit();

编译错误是:

/home/lars/ProgrammierPraktikum/moleculator/implementation/Moleculator/gui_elements/editor.cpp:40:错误:QTextEdit"是一个无法访问的基础'Editor::Editor(std::shared_ptr)::MyTextEdit'QTextEdit* myTextEdit=new MyTextEdit();

/home/lars/ProgrammierPraktikum/moleculator/implementation/Moleculator/gui_elements/editor.cpp:40: error: 'QTextEdit' is an inaccessible base of 'Editor::Editor(std::shared_ptr)::MyTextEdit' QTextEdit* myTextEdit=new MyTextEdit();

实际问题:

如何创建与其超类的指针兼容的匿名子类?

How do I create an anonymous subclass that is compatible to pointers of its superclass ?

推荐答案

你的子类化尝试

class MyTextEdit:protected QTextEdit{
        void focusInEvent();
    };
QTextEdit* myTextEdit=new MyTextEdit();

几乎没问题.

仅仅因为方法受保护并不意味着您应该使用受保护的方式继承.

Just because the method is protected doesn't mean you should inherit with protected.

  • 一个受保护的方法说:这不是我的界面的一部分.除了我之外,没有人应该可以这样称呼它.我将自己称之为(作为事件处理的一部分).该方法可以在子类中被覆盖.
  • 继承受保护说:没有人应该知道这种继承,它是一个实现细节,可能对扩展我的类有用.

您想要常规的公共继承.

You want the regular public inheritance.

class MyTextEdit:public QTextEdit{
        void focusInEvent();
    };
QTextEdit* myTextEdit=new MyTextEdit();

现在您说 MyTextEdit 是 QTextEdit 的替代品.您可能想要添加一个构造函数以将父窗口小部件提供给 MyTextEdit.

Now you are saying that MyTextEdit is a replacement for a QTextEdit. You might want to add a constructor to supply the parent widget to MyTextEdit.

c++ 中没有类似 java 的匿名内部类.

There is no such thing as a java-like anonymous inner class in c++.

这篇关于C++:为事件处理程序创建一个匿名类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:C++:为事件处理程序创建一个匿名类

基础教程推荐