Solved Polymorphism didn't work
-
@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 -
@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
-
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 aPersonnage
. Always. -
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
-
@Zunneh said in Polymorphism didn't work:
moveUp(flash); // this don't work its like i didn't write this command
lawl
andflash
are 2 different items. Since you never give a parent toflash
it remains as a separate top level window and since you never callshow
on it it is never displayed on screen.move
works as expected -
@VRonin Flash is the same object created with lawl (Personnage ) how can i do it ?
-
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)); } }
-
@Zunneh said in Polymorphism didn't work:
Flash is the same object created with lawl
It is not. 2
new
= 2 different objects -
@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 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)); } }
-
thank you guys , it work :)