Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Polymorphism didn't work
Qt 6.11 is out! See what's new in the release blog

Polymorphism didn't work

Scheduled Pinned Locked Moved Solved General and Desktop
22 Posts 6 Posters 7.1k Views 3 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.HilkJ J.Hilk

    @Zunneh
    your problem is that you shadow your base class functions instead of overwriting them.

    IIRC name lookup stops if it finds a name in one of your bases. It won't look beyond in other bases.

    So it finds the base function and does not look for your other implementation.

    IF you really want to go the way of different classes, make your base class function virtual and overwrite them in the derived classes

    Z Offline
    Z Offline
    Zunneh
    wrote on last edited by
    #9

    @J.Hilk i make my base class virtual and i write the same functions in the other class , my problem i think is with constructor , i create the object in my base class (Personnage her ) and the 3 child are without constructor , but the child can't control the object created in the base class , this is my problem , i want to say to the child that they are going to control the object of the class base (the green ball)

    my english is average, please use simple words and try to be the most explicit, thank you

    1 Reply Last reply
    0
    • VRoninV Offline
      VRoninV Offline
      VRonin
      wrote on last edited by
      #10

      1st of all, can you add override to the derived classes to make sure you are indeed reimplementing the virtual methods? https://en.cppreference.com/w/cpp/language/override

      i create the object in my base class

      If you create an object of the base class then it will use the methods defined in the base class, not on the derived ones

      "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
      ~Napoleon Bonaparte

      On a crusade to banish setIndexWidget() from the holy land of Qt

      Z 1 Reply Last reply
      2
      • VRoninV VRonin

        1st of all, can you add override to the derived classes to make sure you are indeed reimplementing the virtual methods? https://en.cppreference.com/w/cpp/language/override

        i create the object in my base class

        If you create an object of the base class then it will use the methods defined in the base class, not on the derived ones

        Z Offline
        Z Offline
        Zunneh
        wrote on last edited by
        #11

        @VRonin said in Polymorphism didn't work:

        https://en.cppreference.com/w/cpp/language/override

        so i shouldn't create it on the base class ?

        my english is average, please use simple words and try to be the most explicit, thank you

        VRoninV 1 Reply Last reply
        0
        • Z Zunneh

          @VRonin said in Polymorphism didn't work:

          https://en.cppreference.com/w/cpp/language/override

          so i shouldn't create it on the base class ?

          VRoninV Offline
          VRoninV Offline
          VRonin
          wrote on last edited by
          #12

          @Zunneh No, you just add the override keyword in the base class to tell the compiler to make 100% sure you are indeed reimplementing a virtual method. It's just a safety feature, no change in code behaviour

          "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
          ~Napoleon Bonaparte

          On a crusade to banish setIndexWidget() from the holy land of Qt

          Z 1 Reply Last reply
          0
          • VRoninV VRonin

            @Zunneh No, you just add the override keyword in the base class to tell the compiler to make 100% sure you are indeed reimplementing a virtual method. It's just a safety feature, no change in code behaviour

            Z Offline
            Z Offline
            Zunneh
            wrote on last edited by
            #13

            @VRonin yeah but where i should create the object ? the green ball , you tell me i can't create it in the base class because it will only use base class methods

            my english is average, please use simple words and try to be the most explicit, thank you

            1 Reply Last reply
            0
            • VRoninV Offline
              VRoninV Offline
              VRonin
              wrote on last edited by
              #14

              members can be created anywhere you want, the base class is fine, what I meant is that if you create Personnage a; it will behave as a Personnage. Always.

              "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
              ~Napoleon Bonaparte

              On a crusade to banish setIndexWidget() from the holy land of Qt

              1 Reply Last reply
              0
              • Z Offline
                Z Offline
                Zunneh
                wrote on last edited by
                #15

                Personnage.h

                 ifndef PERSONNAGE_H
                 #define PERSONNAGE_H
                 #include <QMainWindow>
                 #include <QtWidgets>
                
                 class Personnage:public QWidget
                {
                public: 
                
                Personnage(QWidget *parent=0);
                ~Personnage();
                virtual void moveToUp();
                virtual void moveToLeft();
                virtual void moveToRight();
                virtual void moveToDown();
                
                protected:
                
                  int m_x;
                  int m_y;
                  QLabel *test;
                 };
                
                void moveUp(Personnage *e);
                void moveLeft(Personnage *e);
                void moveRight(Personnage *e);
                void moveDown(Personnage *e);
                
                 #endif // PERSONNAGE_H
                

                Personnage.cpp

                 #include "personnage.h"
                
                 Personnage::Personnage(QWidget *parent):QWidget(parent)
                {
                
                QLabel *test=new QLabel(this);
                test->setFixedSize(30,30);
                QPixmap image5("/Users/mac/Documents/Qt projet/PolymorphysmeV1/bouton-vert.png");
                test->setPixmap(image5);
                
                }
                
                void Personnage::moveToUp()
                {
                
                move(40,m_y);
                
                }
                
                void Personnage::moveToLeft()
                
                {
                
                move(20,m_y);
                
                }
                
                void Personnage::moveToRight()
                
                {
                
                move(0,m_y);
                
                }
                
                void Personnage::moveToDown()
                
                {
                
                move(30,m_y);
                
                }
                
                void moveUp(Personnage *e)
                
                {
                
                e->moveToUp();
                
                }
                
                void moveLeft(Personnage *e)
                
                {
                
                 e->moveToLeft();
                
                }
                
                void moveRight(Personnage *e)
                
                {
                
                e->moveToRight();
                
                }
                
                void moveDown(Personnage *e)
                
                {
                
                e->moveToDown();
                
                }
                

                Now this is the child Flash.h

                #ifndef FLASH_H
                #define FLASH_H
                #include"personnage.h"
                class Flash:public Personnage
                
                {
                
                public:
                void moveToUp() override;
                void moveToLeft() override;
                void moveToRight() override;
                void moveToDown() override;
                
                private:
                
                 };
                
                #endif // FLASH_H
                

                This is the child flash.cpp

                #include "flash.h"
                void Flash::moveToUp()
                {
                move(10,10);
                }
                void Flash::moveToLeft()
                {
                move(m_x+10,m_y);
                }
                void Flash::moveToRight()
                {
                move(m_x-10,m_y);
                }
                void Flash::moveToDown()
                {
                move(m_x,m_y+10);
                }
                

                now in main program MainWindow.cpp

                lawl=new Personnage(this);
                flash =new Flash; 
                moveUp(lawl); //This work
                moveUp(flash); // this don't work its like i didn't write this command
                

                my english is average, please use simple words and try to be the most explicit, thank you

                VRoninV 1 Reply Last reply
                0
                • Z Zunneh

                  Personnage.h

                   ifndef PERSONNAGE_H
                   #define PERSONNAGE_H
                   #include <QMainWindow>
                   #include <QtWidgets>
                  
                   class Personnage:public QWidget
                  {
                  public: 
                  
                  Personnage(QWidget *parent=0);
                  ~Personnage();
                  virtual void moveToUp();
                  virtual void moveToLeft();
                  virtual void moveToRight();
                  virtual void moveToDown();
                  
                  protected:
                  
                    int m_x;
                    int m_y;
                    QLabel *test;
                   };
                  
                  void moveUp(Personnage *e);
                  void moveLeft(Personnage *e);
                  void moveRight(Personnage *e);
                  void moveDown(Personnage *e);
                  
                   #endif // PERSONNAGE_H
                  

                  Personnage.cpp

                   #include "personnage.h"
                  
                   Personnage::Personnage(QWidget *parent):QWidget(parent)
                  {
                  
                  QLabel *test=new QLabel(this);
                  test->setFixedSize(30,30);
                  QPixmap image5("/Users/mac/Documents/Qt projet/PolymorphysmeV1/bouton-vert.png");
                  test->setPixmap(image5);
                  
                  }
                  
                  void Personnage::moveToUp()
                  {
                  
                  move(40,m_y);
                  
                  }
                  
                  void Personnage::moveToLeft()
                  
                  {
                  
                  move(20,m_y);
                  
                  }
                  
                  void Personnage::moveToRight()
                  
                  {
                  
                  move(0,m_y);
                  
                  }
                  
                  void Personnage::moveToDown()
                  
                  {
                  
                  move(30,m_y);
                  
                  }
                  
                  void moveUp(Personnage *e)
                  
                  {
                  
                  e->moveToUp();
                  
                  }
                  
                  void moveLeft(Personnage *e)
                  
                  {
                  
                   e->moveToLeft();
                  
                  }
                  
                  void moveRight(Personnage *e)
                  
                  {
                  
                  e->moveToRight();
                  
                  }
                  
                  void moveDown(Personnage *e)
                  
                  {
                  
                  e->moveToDown();
                  
                  }
                  

                  Now this is the child Flash.h

                  #ifndef FLASH_H
                  #define FLASH_H
                  #include"personnage.h"
                  class Flash:public Personnage
                  
                  {
                  
                  public:
                  void moveToUp() override;
                  void moveToLeft() override;
                  void moveToRight() override;
                  void moveToDown() override;
                  
                  private:
                  
                   };
                  
                  #endif // FLASH_H
                  

                  This is the child flash.cpp

                  #include "flash.h"
                  void Flash::moveToUp()
                  {
                  move(10,10);
                  }
                  void Flash::moveToLeft()
                  {
                  move(m_x+10,m_y);
                  }
                  void Flash::moveToRight()
                  {
                  move(m_x-10,m_y);
                  }
                  void Flash::moveToDown()
                  {
                  move(m_x,m_y+10);
                  }
                  

                  now in main program MainWindow.cpp

                  lawl=new Personnage(this);
                  flash =new Flash; 
                  moveUp(lawl); //This work
                  moveUp(flash); // this don't work its like i didn't write this command
                  
                  VRoninV Offline
                  VRoninV Offline
                  VRonin
                  wrote on last edited by
                  #16

                  @Zunneh said in Polymorphism didn't work:

                  moveUp(flash); // this don't work its like i didn't write this command

                  lawl and flash are 2 different items. Since you never give a parent to flash it remains as a separate top level window and since you never call show on it it is never displayed on screen. move works as expected

                  "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                  ~Napoleon Bonaparte

                  On a crusade to banish setIndexWidget() from the holy land of Qt

                  Z 1 Reply Last reply
                  2
                  • VRoninV VRonin

                    @Zunneh said in Polymorphism didn't work:

                    moveUp(flash); // this don't work its like i didn't write this command

                    lawl and flash are 2 different items. Since you never give a parent to flash it remains as a separate top level window and since you never call show on it it is never displayed on screen. move works as expected

                    Z Offline
                    Z Offline
                    Zunneh
                    wrote on last edited by
                    #17

                    @VRonin Flash is the same object created with lawl (Personnage ) how can i do it ?

                    my english is average, please use simple words and try to be the most explicit, thank you

                    J.HilkJ VRoninV 2 Replies Last reply
                    0
                    • Z Zunneh

                      @VRonin Flash is the same object created with lawl (Personnage ) how can i do it ?

                      J.HilkJ Offline
                      J.HilkJ Offline
                      J.Hilk
                      Moderators
                      wrote on last edited by J.Hilk
                      #18

                      @Zunneh

                      flash =new Flash(this);
                      auto pers = qobject_cast<Personage*>(flash);
                      if(pers)
                          pers->moveUp();
                      flash->moveUp();
                      

                      really, you should consider an other approach:

                      class myDot :  public QLabel
                      {
                       enum MoveSpeed{
                           Personage = 1,
                           Tortue = 2,
                           Flash  = 10
                      }; E_ENUM(MoveSpeed)
                      .....
                      
                      public: 
                      void moveToUp(MoveSpeed speed){
                           move (m_x, m_y - static_cast<int>(speed));
                      }
                      
                      }
                      

                      Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                      Q: What's that?
                      A: It's blue light.
                      Q: What does it do?
                      A: It turns blue.

                      1 Reply Last reply
                      3
                      • Z Zunneh

                        @VRonin Flash is the same object created with lawl (Personnage ) how can i do it ?

                        VRoninV Offline
                        VRoninV Offline
                        VRonin
                        wrote on last edited by
                        #19

                        @Zunneh said in Polymorphism didn't work:

                        Flash is the same object created with lawl

                        It is not. 2 new = 2 different objects

                        "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                        ~Napoleon Bonaparte

                        On a crusade to banish setIndexWidget() from the holy land of Qt

                        Z 1 Reply Last reply
                        2
                        • VRoninV VRonin

                          @Zunneh said in Polymorphism didn't work:

                          Flash is the same object created with lawl

                          It is not. 2 new = 2 different objects

                          Z Offline
                          Z Offline
                          Zunneh
                          wrote on last edited by Zunneh
                          #20

                          @VRonin yeah you are right i tried it and its 2 different objects , so i forget something , i'm moving 2 Different objects and my goal is to move one object with different form

                          my english is average, please use simple words and try to be the most explicit, thank you

                          1 Reply Last reply
                          0
                          • SGaistS SGaist

                            Technically you don't need three subclasses, you can just have setters which set the speed of your "Personnage" class.

                            VRoninV Offline
                            VRoninV Offline
                            VRonin
                            wrote on last edited by
                            #21

                            my goal is to move one object with different form

                            @SGaist said in Polymorphism didn't work:

                            Technically you don't need three subclasses, you can just have setters which set the speed of your "Personnage" class.

                            @J.Hilk said in Polymorphism didn't work:

                            really, you should consider an other approach:

                            class myDot :  public QLabel
                            {
                             enum MoveSpeed{
                                 Personage = 1,
                                 Tortue = 2,
                                 Flash  = 10
                            }; E_ENUM(MoveSpeed)
                            .....
                            
                            public: 
                            void moveToUp(MoveSpeed speed){
                                 move (m_x, m_y - static_cast<int>(speed));
                            }
                            
                            }
                            

                            "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                            ~Napoleon Bonaparte

                            On a crusade to banish setIndexWidget() from the holy land of Qt

                            1 Reply Last reply
                            3
                            • Z Offline
                              Z Offline
                              Zunneh
                              wrote on last edited by
                              #22

                              thank you guys , it work :)

                              my english is average, please use simple words and try to be the most explicit, thank you

                              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