开发者

can you have a private member of the same class as the base class you're inheriting?

Im using the Qt library. I'm currently trying to create my own QDockWidget (the class MY class is inheriting). Right now MY class has an ptr to QDockWidget. Does this even make sense? is that a legal statement? is there a better way to separate the QDockWidget from the rest of my program in Qt? Im a little lost on how to implement a new Dock Widget. Here is a copy of MY QDockWidget class:

#ifndef DOC_MENU_WIDGET_H #define DOC_MENU_WIDGET_H #include "App_interface.h" #include <QObject> #include <QWidget> #include <QDockWidge开发者_StackOverflowt> class Doc_menu_widget : public QWidget { //Q_OBJECT public: Doc_menu_widget(App_interface *parent); ~Doc_menu_widget(); private: QDockWidget *dock_widget; }; #endif


You seem to be confusing the IS-A relationship and the HAS-A relationship.

IS-A relations are implemented by inheritance. For instance, a QWidget IS-A QObject.

HAS-A relations are implemted by members. For instance, a QWidget HAS-A size.

Now, what's the relation between the class you are trying to develop and a QDockWidget? That will tell you which of the two you should choose.


Shouldn't you be doing something like this?

class Doc_menu_widget : public QDockWidget
{
    // ...
};

Subclassing QWidget and then having a private QDockWidget attribute of course does make sense, but it will probably not help you for implementing "your own dock widget" (as long as I understand it correctly). You only get the original dock widget this way and you can place it somewhere inside your new widget.

Alternatively, if you want to implement an equivalent of QDockWidget from scratch, you probably don't need the private QDockWidget.


Sure, why not. And if you are just using a ptr to a base class object, you don't have any problems at all.

Why aren't you deriving directly from QDockWidget in the first place?

Do you want to express:

  • (a) Doc_menu_widget IS-A QDockWidget --> derive

  • (b) IS-IMPLEMENTED_IN_TERMS-OF QDockWidget (same as PERFORMS-LIKE QDockWidget) --> use a private member


If you just need to create a QDockWidget* for your QMainWindow, most of the times you can simply create an instance and use it right away:

QDockWidget* dock = new QDockWidget(this);
dock->setWindowTitle("My Dock Widget");
addDockWidget(Qt::LeftDockWidgetArea, dock, Qt::Vertical);

If instead you want to create a customized QDockWidget with different behaviour or appearance, then it's probably more convenient to inherit from QDockWidget:

#include <QDockWidget>

class MyDockWidget : public QDockWidget
{
Q_OBJECT
    // ...
};

Having a separate class with a QDockWidget* private member is of course entirely possible and legitimate, but it's not the most common choice in this situation. The reason is that you most probably want your class to be a variation of a QDockWidget (is-a relationship) meaning that it should have all its public methods and its instances should be able to passed to methods asking QDockWidget instances (for example, you want to be able to add one to a QMainWindow).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜