Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Special Interest Groups
  3. C++ Gurus
  4. Simple question regarding base and derivated class functions
Qt 6.11 is out! See what's new in the release blog

Simple question regarding base and derivated class functions

Scheduled Pinned Locked Moved C++ Gurus
8 Posts 4 Posters 3.7k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    JoseAbias
    wrote on last edited by
    #1

    Dear C++ Gurus

    Please help!

    I have a base class and a derivated class (from base).
    I need to use a function that must be on the derivated class only (the "void Derive::RunThisFunc()" ).

    I don't know why, but I can only use it if I have them on the Base class.

    Can you tell me why it's not working if I call the function in the derive class?

    @
    //--------------- Base.h ---------------

    //Base class
    class Base: public QWidget
    {
    Q_OBJECT
    (...)
    private:
    QPushButton *myButton;
    char matrixPosition[9];

    public:
    char GetMatrixPos(unsigned char pos) { return matrixPosition[pos];}
    void SetMatrixPos(unsigned char pos, char value) { matrixPosition[pos] = value;}

    (...)
    }

    //--------------- Base.cpp ---------------

    Base::Base(QWidget *parent) : QWidget(parent)
    {
    memset(&matrixPosition, -1, sizeof(matrixPosition)); //set all the matrix to -1

    myButton = new QPushButton("OK");
    myButton->show();

    QGridLayout *mainLayout = new QGridLayout;
    mainLayout->addWidget(myButton, 0, 0);
    setLayout(mainLayout);

    connect(myButton, SIGNAL(clicked()), this, SLOT(myButtonPressed()));
    }

    void Base::myButtonPressed()
    {
    SetMatrixPos(0, 4); //set position 0 to value 4

    RunThisFunc(); //called from base class : WORKS!!

        Derive d;
        d.RunThisFunc(); //called from derive class :  DOESN'T WORK! :(
    

    }

    void Base::RunThisFunc()
    {
    if(GetMatrixPos(0) == 4)
    QMessageBox::information(this, tr("I WANT TO SEE THIS BOX"), tr("FINISHED"));

    }

    //---------------- Derive.h -----------------

    #include "Base.h"

    class Derive: public Base //inheritates base class!!
    {
    public:
    Derive();
    void RunThisFunc();
    };

    //---------------- Derive.cpp -----------------

    #include "Base.h"
    #include "Derive.h"

    void Derive::RunThisFunc()
    {
    if(GetMatrixPos(0) == 4)
    QMessageBox::information(this, tr("I WANT TO SEE THIS BOX"), tr("FINISHED"));

    }
    @

    Thank you in advance!!

    1 Reply Last reply
    0
    • K Offline
      K Offline
      kaivolde
      wrote on last edited by
      #2

      How do you call the method RunThisFunc()?

      If you put a new method to the class Derive, it is callable from an object of the class Derive.

      class Derive : public Base
      {
      public:
      void myTest();
      };

      Derive d;
      d.myTest();

      Does it solve your problem?

      1 Reply Last reply
      0
      • J Offline
        J Offline
        JoseAbias
        wrote on last edited by
        #3

        That's what I'm doing... (see line 43 and 44 above).

        I'm calling the RunThisFunc(); from the base class, which is inside the derived class and uses members of the base class... :(
        @
        Derive d;
        d.RunThisFunc(); //called from derive class : DOESN'T WORK! :(
        @

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Hi and welcome to devnet,

          If you only have it in the derived class how could the base know about it ?

          You seem to be looking for a "pure virtual function":http://www.learncpp.com/cpp-tutorial/126-pure-virtual-functions-abstract-base-classes-and-interface-classes/

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0
          • J Offline
            J Offline
            JoseAbias
            wrote on last edited by
            #5

            Thank you SGaist for the reply. I don't think I need a pure virtual function since I don't want to declare the function "RunThisFunc()" on the base class. I just did that to show you that it's working if I declare it on the base class.

            What I want is to call a function from the derivated class which inside uses the members of the base class. Like this bellow (i just removed some lines to be more simples to understand my problem)

            @
            //--------------- Base.h ---------------

            //Base class
            class Base: public QWidget
            {
            Q_OBJECT
            (...)
            private:
            QPushButton *myButton;
            char matrixPosition[9];

            public:
            char GetMatrixPos(unsigned char pos) { return matrixPosition[pos];}
            void SetMatrixPos(unsigned char pos, char value) { matrixPosition[pos] = value;}

            (...)
            }

            //--------------- Base.cpp ---------------

            Base::Base(QWidget *parent) : QWidget(parent)
            {
            (...)

            connect(myButton, SIGNAL(clicked()), this, SLOT(myButtonPressed()));
            }

            void Base::myButtonPressed()
            {
            SetMatrixPos(0, 4); //set position 0 to value 4

            Derive d;
            d.RunThisFunc(); //called from derive class : DOESN'T WORK! :( <--------- WHY??????????????
            }

            //---------------- Derive.h -----------------

            #include "Base.h"

            class Derive: public Base //inheritates base class!!
            {
            public:
            void RunThisFunc();
            };

            //---------------- Derive.cpp -----------------

            #include "Base.h"
            #include "Derive.h"

            void Derive::RunThisFunc()
            {
            if(GetMatrixPos(0) == 4) //Function from the base class...it should be TRUE.....
            QMessageBox::information(this, tr("I WANT TO SEE THIS BOX"), tr("FINISHED")); //NEVER SEE IT

            }
            @

            1 Reply Last reply
            0
            • K Offline
              K Offline
              KA51O
              wrote on last edited by
              #6

              You want to have the chicken that hatched out of an egg to lay the egg it hatched from.

              1 Reply Last reply
              0
              • J Offline
                J Offline
                JoseAbias
                wrote on last edited by
                #7

                That's exacly what I want!!
                Is that possible? If not, what should I do to remediate this situation?

                1 Reply Last reply
                0
                • SGaistS Offline
                  SGaistS Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  You can't like that.

                  d is a new object that doesn't know anything of the Base object that you are using to create it.

                  You call d.SetMatrixPos(0, 4); to get your dialog to show.

                  Interested in AI ? www.idiap.ch
                  Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                  1 Reply Last reply
                  0

                  • Login

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • Users
                  • Groups
                  • Search
                  • Get Qt Extensions
                  • Unsolved