指针继承 [英] Dpointer inheritance

查看:75
本文介绍了指针继承的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用以下示例中的qt示例学习如何从低音类继承d指针:
http://qt-project.org/wiki/Dpointer#7969fa90723037d326b77fb11381044e

I am trying to learn how to inherit d-pointers from a bass class using the qt example from
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

#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

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) {
 }

当我在g ++中编译它时,会出现此错误

When I go to compile this in g++ it gives this error

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

在clang中,这会或多或少地导致相同的错误,因此问题必须出在代码中,但我不知道在哪里。

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.

推荐答案

LabelPrivate 是从 WidgetPrivate 继承的,后者没有默认构造函数,只有具有默认值的构造函数小部件* 。编译器为 LabelPrivate 生成的默认构造函数将尝试默认构造其基类( WidgetPrivate ),从而导致错误。您的类定义必须是这样的:

LabelPrivate is inheriting from WidgetPrivate and the latter doesn't have a default constructor, only a constructor that takes a Widget *. The compiler generated default constructor for LabelPrivate will attempt to default construct its base class (WidgetPrivate) resulting in the error. Your class definition needs to be something like this:

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

这篇关于指针继承的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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