【问题标题】:Get member of parent struct获取父结构的成员
【发布时间】:2016-02-24 08:51:32
【问题描述】:

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

在一个类中,我有一个变量、一个结构和一个结构的声明。在该结构中是构造函数、变量、结构和该结构的声明。在那个结构里面是一个构造函数。

Mother > Daughter > GDaughter又名class > struct > struct也是如此

妈妈.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

目标是动态创建“子”结构。所以一个父结构可能有 0 个子结构,1 个子结构,甚至 100 个子结构。这就是我使用结构而不是派生类的原因。除了无法访问“父”变量的问题之外,此设置看起来可行。

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

主窗口.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();
}

主窗口.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
}

【问题讨论】:

  • 您的设计令人困惑。 age是母亲的年龄还是女儿的年龄?如果您只使用函数和私有成员变量将值传递给其他类,那就更好了。似乎也没有理由使用嵌套结构。
  • 我知道,这是一个用于测试不同事物的 SSCE。这不是我的实际程序。母亲、女儿和女儿只是更简单的查看嵌套的方式......没关系......
  • 我明白了。我被你为这个特定例子选择的名字吓坏了。

标签: c++ qt class struct


【解决方案1】:

保留指向嵌套对象中父类对象的指针(例如,如果您使用智能指针,则为 std::weak_ptr)。

    explicit MainWindow(QWidget *parent = 0);

看这里的Qt代码,你在构造函数中传递了指向父对象的指针, 如果 parent 为 nullptr,则表示 MainWindow 没有父对象,您也可以保留 指向 MainWindow 父对象的指针,然后将其静态转换为确切的类

     #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;
    };

【讨论】:

  • 您好,恐怕我不明白您的回答。你能扩展或改写它吗?
  • 成功了。非常感谢!我会在一秒钟内用确切的变化更新我的答案。快速提问,GDaughter 如何访问班级(母亲)变量年龄?因为我的实际程序有不止 1 个嵌套。它大约有 5 个。有没有一种优雅的方法可以做到这一点?基本上比只有1个父母更高。我会创建一堆 getter 函数吗?
  • 添加 public 方法 parent() 将返回对象的父对象,然后调用它 obj->Parent()->Parent()->Parent()->age();很高兴我能帮忙:)
  • 非常感谢! :)
  • 也不要忘记在析构函数中删除子对象,使用智能指针也是一个好习惯 - 基类将子对象保留在 std::shared_ptr 中,子类将weak_ptr保留在基类中(所以你不会有这种情况2 个对象保持 shared_ptr 相互引用,不能删除)。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多