Simple question regarding base and derivated class functions
-
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 -1myButton = 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 4RunThisFunc(); //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!!
-
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?
-
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! :(
@ -
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/
-
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 4Derive 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}
@ -
You want to have the chicken that hatched out of an egg to lay the egg it hatched from.
-
That's exacly what I want!!
Is that possible? If not, what should I do to remediate this situation? -
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.