获取父结构的成员

时间:2016-02-24 08:51:33

标签: c++ qt class struct

我已经创建了一个SSCE来更好地解释这一点。

在类中,我有一个变量,一个结构和一个struct的声明。在该结构中是构造函数,变量,结构和该结构的声明。在THAT结构中是一个构造函数。

所以,Mother > Daughter > GDaughter又称class > struct > struct

mother.h

#ifndef MOTHER_H
#define MOTHER_H

#include <QSplitter>

class Mother : public QSplitter
{
    Q_OBJECT
public:
    Mother(int);
    int age;

    struct Daughter
    {
        Daughter();
        int height1;

        struct GDaughter
        {
            GDaughter();
        };
        GDaughter *kate;
    };
    Daughter *tina;
};

#endif // MOTHER_H

现在让我们来看看构造函数/源代码。这就是我的问题。

mother.cpp

#include "mother.h"
#include <QDebug>

Mother::Mother(int a)
{
    age = a;
    tina = new Daughter();
}

Mother::Daughter::Daughter()
{
    qDebug() << age;   //Not going to work... I get it. Daughter isnt a derived class

    height1 = 10;
    kate = new GDaughter();
}

Mother::Daughter::GDaughter::GDaughter()
{
    qDebug() << height1;   //Why not? The GDaughter instance is a member of Daughter!
}

两条qDebug()行抛出is not a type name, static, or enumerator

目标是创造孩子&#34;结构动态。因此父结构可能有0个子结构,1个子结构,甚至100个子结构。这就是我使用结构而不是派生类的原因。这个设置看起来像它可以工作,除了&#34; parent&#34;无法访问变量。

无论如何我会包含其他文件:

mainwindow.cpp

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "mother.h"
#include <QDebug>

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    mom = new Mother(50);
}

MainWindow::~MainWindow()
{
    delete ui;
}

的main.cpp

#include "mainwindow.h"
#include <QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MainWindow w;
    w.show();

    return a.exec();
}

mainwindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include "mother.h"

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();
    Mother *mom;

private:
    Ui::MainWindow *ui;
};

#endif // MAINWINDOW_H

如果我误解了如何制作这样的东西,请告诉我。

感谢您的时间。

回答后

在mother.h中我添加了父指针:

struct Daughter
{
    Daughter(Mother *p); //Here
    int height1;
    struct GDaughter
    {
        GDaughter(Daughter *p); //And here
    };
    GDaughter *kate;
};

在mother.cpp中我填写了所需的代码:

Mother::Mother(int a)
{
    age = a;
    tina = new Daughter(this); //Here
}

Mother::Daughter::Daughter(Mother *m) //Here
{
    qDebug() << m->age; //Here

    height1 = 10;
    kate = new GDaughter(this); //Here
}

Mother::Daughter::GDaughter::GDaughter(Daughter *d) //Here
{
    qDebug() << d->height1; //Here
}

1 个答案:

答案 0 :(得分:2)

将指针(std :: weak_ptr,如果你使用智能指针)保存到嵌套对象中的父类对象。

    explicit MainWindow(QWidget *parent = 0);

在这里查看Qt代码,将指针传递给构造函数中的父对象, 如果parent是nullptr,这意味着MainWindow没有父对象,也可以保留 指向MainWindow父对象的指针,然后将static_cast指向精确类

     #include <iostream>
    class A {
        class B{
        public:
            B(A *p):parent(p) { parent->hello(); }
        private:
            A *parent;
        };

    public:
        A() { b = new B(this); }

       void hello() { std::cout << "hello world" << std::endl; }
    private:
       B *b;
    };