Help global variable



  • I believe this is a matter purely C + +, since my experience with C + + is small.

    I have the following code

    @
    /*

    • dynamicuitest.hpp
    • This file is part of DynamicUi.so
    • Copyright (C) 2014 - Matheus Saraiva
    • DynamicUi.so is free software; you can redistribute it and/or modify
    • it under the terms of the GNU General Public License as published by
    • the Free Software Foundation; either version 2 of the License, or
    • (at your option) any later version.
    • DynamicUi.so is distributed in the hope that it will be useful,
    • but WITHOUT ANY WARRANTY; without even the implied warranty of
    • MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    • GNU General Public License for more details.
    • You should have received a copy of the GNU General Public License
    • along with DynamicUi.so. If not, see http://www.gnu.org/licenses/.
      */

    #ifndef DYNAMICUITEST_H

    define DYNAMICUITEST_H

    #include <QString>
    #include <QtTest>
    #include "dynamicui.hpp"

    class DynamicUiTest : public QObject
    {
    Q_OBJECT

    public:
    DynamicUiTest();

    private Q_SLOTS:
    void testCreateUi();
    void testCreateUi_data();
    };
    #endif
    @

    @
    /*

    • dynamicuitest.cpp
    • This file is part of DynamicUi.so
    • Copyright (C) 2014 - Matheus Saraiva
    • DynamicUi.so is free software; you can redistribute it and/or modify
    • it under the terms of the GNU General Public License as published by
    • the Free Software Foundation; either version 2 of the License, or
    • (at your option) any later version.
    • DynamicUi.so is distributed in the hope that it will be useful,
    • but WITHOUT ANY WARRANTY; without even the implied warranty of
    • MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    • GNU General Public License for more details.
    • You should have received a copy of the GNU General Public License
    • along with DynamicUi.so. If not, see http://www.gnu.org/licenses/.
      */

    #include "dynamicuitest.hpp"

    QWidget *widtest; // Pointer for object recepted from DynamicUi

    DynamicUiTest::DynamicUiTest()
    {
    }

    void DynamicUiTest::testCreateUi()
    {
    QFETCH(QString, file_ui);
    QFETCH(bool, widget);
    DynamicUi lui(file_ui); // DynamicUi object
    widtest = lui.createUi();

    QCOMPARE(widtest->isWidgetType(), widget); // Compare DynamicUi::createUi() return with QWidget type
    }

    void DynamicUiTest::testCreateUi_data()
    {
    QTest::addColumn<QString>("file_ui");
    QTest::addColumn<bool>("widget");

    QTest::newRow("Valid File") << "test.ui" << true; // Valid File
    QTest::newRow("Inexistent File") << "inexistent.ui" << true; // Inexistent File
    QTest::newRow("Invalid File") << "test_text.ui" << true; // Invalid File
    QTest::newRow("Empty Ui File") << "testempty.ui" << true; // Empty File
    delete widtest;
    }
    @

    If I move "QWidget * widtest" of dynamicuitest.cpp to dynamicuitest.hpp. Becoming a global variable,

    @
    /*

    • dynamicuitest.hpp
    • This file is part of DynamicUi.so
    • Copyright (C) 2014 - Matheus Saraiva
    • DynamicUi.so is free software; you can redistribute it and/or modify
    • it under the terms of the GNU General Public License as published by
    • the Free Software Foundation; either version 2 of the License, or
    • (at your option) any later version.
    • DynamicUi.so is distributed in the hope that it will be useful,
    • but WITHOUT ANY WARRANTY; without even the implied warranty of
    • MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    • GNU General Public License for more details.
    • You should have received a copy of the GNU General Public License
    • along with DynamicUi.so. If not, see http://www.gnu.org/licenses/.
      */

    #ifndef DYNAMICUITEST_H

    define DYNAMICUITEST_H

    #include <QString>
    #include <QtTest>
    #include "dynamicui.hpp"

    class DynamicUiTest : public QObject
    {
    Q_OBJECT

    public:
    DynamicUiTest();

    private:
    QWidget *widtest;

    private Q_SLOTS:
    void testCreateUi();
    void testCreateUi_data();
    };
    #endif
    @

    fail at runtime.



  • Ok, now I understand the difference Global Variable and Variable of Instance.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.