简体   繁体   English

指针继承

[英]Dpointer inheritance

I am trying to learn how to inherit d-pointers from a bass class using the qt example from 我正在尝试使用以下示例中的qt学习如何从低音类继承d指针
http://qt-project.org/wiki/Dpointer#7969fa90723037d326b77fb11381044e http://qt-project.org/wiki/Dpointer#7969fa90723037d326b77fb11381044e

I have copied it verbatim from the web site with only a slight modifaction so that the code looks like this: 我已经从网站上逐字复制了内容,仅作了少许修改,因此代码如下所示:

widget.h widget.h

#ifndef WIDGET_H
#define WIDGET_H

// FWD
class WidgetPrivate;
// END

class Widget {
   public:
     Widget();
   protected:
     // only sublasses may access the below
     Widget(WidgetPrivate &d); // allow subclasses to initialize with their own concrete Private
     WidgetPrivate *d_ptr;
 };

 #endif /* WIDGET_H */

widget_p.h widget_p.h

#ifndef WIDGET_P_H
#define WIDGET_P_H

#include <string>

#include "widget.h"

// FWD
class Widget;
// End

typedef int Rect;
typedef std::string String;

struct WidgetPrivate 
{
    WidgetPrivate(Widget *q) : q_ptr(q) { } // constructor that initializes the q-ptr
    Widget *q_ptr; // q-ptr that points to the API class
    Rect geometry;
    String stylesheet;
};

#endif /* WIDGET_P_H */

widget.cpp widget.cpp

#include "widget.h"

Widget::Widget()
      : d_ptr(new WidgetPrivate(this)) {
}

Widget::Widget(WidgetPrivate &d)
      : d_ptr(&d) {
}

label.h 标签

#ifndef LABEL_H
#define LABEL_H

#include "widget.h"

//FWD
class LabelPrivate;
//END

class Label : public Widget {
  public:
    Label();

  protected:
     Label(LabelPrivate &d); // allow Label subclasses to pass on their Private
  // notice how Label does not have a d_ptr! It just uses Widget's d_ptr.
};

#endif /* LABEL_H */

label.cpp 标签

#include "label.h"
#include "widget.h"
#include "widget_p.h"

 struct LabelPrivate : public WidgetPrivate 
 {        
        String text;
 };

 Label::Label()
    : Widget(*new LabelPrivate) // initialize the d-pointer with our own Private 
 {
 }

 Label::Label(LabelPrivate &d)
    : Widget(d) {
 }

When I go to compile this in g++ it gives this error 当我在g ++中编译它时,出现此错误

label.cpp:5:11: error: no matching function for call to ‘WidgetPrivate::WidgetPrivate()’

I have tried this in clang and I get more or less the same error so the problem has to be in the code but I have no idea where. 我已经在clang中尝试了此操作,但我得到的错误大致相同,因此问题必须出在代码中,但我不知道在哪里。

LabelPrivate derives publically from WidgetPrivate and you do not call appropriate argument taking constructor( WidgetPrivate(Widget *) ) for WidgetPrivate in it's member initialization list. LabelPrivate从公开派生WidgetPrivate ,你不叫适当的参数以构造( WidgetPrivate(Widget *)为) WidgetPrivate在它的成员初始化列表。

You either call the appropriate constructor or provide a constructor for WidgetPrivate which takes no arguments. 您可以调用适当的构造函数,或者为WidgetPrivate提供一个不带参数的构造函数。

Note that if you provide any constructor for your class the compiler does not provide for the default no argument constructor, rationale applied is that since you need to define a constructor you probably need to define each one of them yourself. 请注意,如果为类提供任何构造函数,则编译器将不提供默认的无参数构造函数,其基本原理是,由于需要定义一个构造函数,因此可能需要自己定义每个构造函数。

LabelPrivate is inheriting from WidgetPrivate and the latter doesn't have a default constructor, only a constructor that takes a Widget * . LabelPrivateWidgetPrivate继承,后者没有默认构造函数,只有一个带有Widget *的构造函数。 The compiler generated default constructor for LabelPrivate will attempt to default construct its base class ( WidgetPrivate ) resulting in the error. 编译器为LabelPrivate生成的默认构造函数将尝试默认构造其基类( WidgetPrivate ),从而导致错误。 Your class definition needs to be something like this: 您的类定义必须是这样的:

struct LabelPrivate : public WidgetPrivate 
{      
  LabelPrivate( Widget *w ) : WidgetPrivate( w ) {}  
  String text;
};

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

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