KillGabio
24th July 2013, 06:57
Hi everyone, I have the following methods in my base class:
#ifndef WIDGETFACTURACION_H
#define WIDGETFACTURACION_H
#include <QWidget>
namespace Ui {
class widgetFacturacion;
}
class widgetFacturacion : public QWidget
{
Q_OBJECT
public:
explicit widgetFacturacion(QWidget *parent = 0, int handler = 0, int tipo_factura = 0, int tipo_ticket = 0, int nro_tique = 0);
~widgetFacturacion();
protected:
Ui::widgetFacturacion *ui;
.
.
.
void conectar ();
virtual void ingresar_valores_tabla (QString precio);
virtual void inicializar (int tipo_factura);
protected slots:
virtual void on_cancelar_clicked();
virtual void on_realizar_venta_clicked();
signals:
void widgetClosed (QString );
};
#endif // WIDGETFACTURACION_H
the derived one implements the virtual methods:
#ifndef WIDGETFACTBLACK_H
#define WIDGETFACTBLACK_H
#include "widgetfacturacion.h"
namespace Ui {
class WidgetFactBlack;
}
class WidgetFactBlack : public widgetFacturacion
{
Q_OBJECT
public:
explicit WidgetFactBlack(QWidget *parent, int handler, int tipo_factura, int tipo_ticket=0, int nro_tique=0);
~WidgetFactBlack();
protected:
void ingresar_valores_tabla (QString precio);
void inicializar (int tipo_factura);
protected slots:
void on_cancelar_clicked();
void on_realizar_venta_clicked();
};
#endif // WIDGETFACTBLACK_H
And the .cpp of my derived class:
#include "widgetfactblack.h"
#include "ui_widgetfacturacion.h"
#include "global_variables.h"
WidgetFactBlack::WidgetFactBlack(QWidget *parent, int handler, int tipo_factura, int tipo_ticket, int nro_tique) :
widgetFacturacion(parent, handler, tipo_factura, tipo_ticket, nro_tique)
{
}
WidgetFactBlack::~WidgetFactBlack()
{
}
//---------------------------Inicializamos todo luego de seleccionado el tipo de factura
void WidgetFactBlack::inicializar (int tipo_factura){
qDebug()<< "Entre al metodo en black inicializar";
ui->setupUi(this);
ui->label_descripcion->clear();
.
.
.
}
.
.
.
The problem comes when from the constructor of the base class i want to call the method of the derived class (method inicializar) posted before. Because program is not calling the derived method, instead it executes the one of the base class, even though it is defined by the derived class. I dont know if this is because it is called from within the constructor of the base class...Wish I knew.
base class .cpp
widgetFacturacion::widgetFacturacion(QWidget *parent, int handler, int tipo_factura, int tipo_ticket, int nro_tique) :
QWidget(parent),
ui(new Ui::widgetFacturacion)
{
this->conectar ();
controlador = new ComControlador (handler);
if (tipo_factura >0){
SeleccionarCliente * cliente = new SeleccionarCliente (0);
cliente->setWindowTitle ("Seleccionar Cliente");
connect (cliente, SIGNAL(cliente_seleccionado(QStringList)), this, SLOT(cargar_cliente(QStringList)));
cliente->show ();
}else inicializar (tipo_factura); //PROBLEM, callls the base class method not looking into the derived one
}
void widgetFacturacion::inicializar (int tipo_factura)
{
qDebug()<<"hi";
ui->setupUi(this);
ui->label_descripcion->clear();
}
All the other virtual methods defined in the derived class are called correctly. Only the one called from the constructor gives me problems.
Sorry if this is too basic.
Thank you all in advance.
My bad, I understood now:
Even though it's a virtual function, the base's version will get called since the derived class isn't fully constructed yet. The base class constructor is called before the derived class constructor, so if the derived virtual function were to get called, it would be with an incompletely-initialized instance - a possible (probably) recipe for disaster.
#ifndef WIDGETFACTURACION_H
#define WIDGETFACTURACION_H
#include <QWidget>
namespace Ui {
class widgetFacturacion;
}
class widgetFacturacion : public QWidget
{
Q_OBJECT
public:
explicit widgetFacturacion(QWidget *parent = 0, int handler = 0, int tipo_factura = 0, int tipo_ticket = 0, int nro_tique = 0);
~widgetFacturacion();
protected:
Ui::widgetFacturacion *ui;
.
.
.
void conectar ();
virtual void ingresar_valores_tabla (QString precio);
virtual void inicializar (int tipo_factura);
protected slots:
virtual void on_cancelar_clicked();
virtual void on_realizar_venta_clicked();
signals:
void widgetClosed (QString );
};
#endif // WIDGETFACTURACION_H
the derived one implements the virtual methods:
#ifndef WIDGETFACTBLACK_H
#define WIDGETFACTBLACK_H
#include "widgetfacturacion.h"
namespace Ui {
class WidgetFactBlack;
}
class WidgetFactBlack : public widgetFacturacion
{
Q_OBJECT
public:
explicit WidgetFactBlack(QWidget *parent, int handler, int tipo_factura, int tipo_ticket=0, int nro_tique=0);
~WidgetFactBlack();
protected:
void ingresar_valores_tabla (QString precio);
void inicializar (int tipo_factura);
protected slots:
void on_cancelar_clicked();
void on_realizar_venta_clicked();
};
#endif // WIDGETFACTBLACK_H
And the .cpp of my derived class:
#include "widgetfactblack.h"
#include "ui_widgetfacturacion.h"
#include "global_variables.h"
WidgetFactBlack::WidgetFactBlack(QWidget *parent, int handler, int tipo_factura, int tipo_ticket, int nro_tique) :
widgetFacturacion(parent, handler, tipo_factura, tipo_ticket, nro_tique)
{
}
WidgetFactBlack::~WidgetFactBlack()
{
}
//---------------------------Inicializamos todo luego de seleccionado el tipo de factura
void WidgetFactBlack::inicializar (int tipo_factura){
qDebug()<< "Entre al metodo en black inicializar";
ui->setupUi(this);
ui->label_descripcion->clear();
.
.
.
}
.
.
.
The problem comes when from the constructor of the base class i want to call the method of the derived class (method inicializar) posted before. Because program is not calling the derived method, instead it executes the one of the base class, even though it is defined by the derived class. I dont know if this is because it is called from within the constructor of the base class...Wish I knew.
base class .cpp
widgetFacturacion::widgetFacturacion(QWidget *parent, int handler, int tipo_factura, int tipo_ticket, int nro_tique) :
QWidget(parent),
ui(new Ui::widgetFacturacion)
{
this->conectar ();
controlador = new ComControlador (handler);
if (tipo_factura >0){
SeleccionarCliente * cliente = new SeleccionarCliente (0);
cliente->setWindowTitle ("Seleccionar Cliente");
connect (cliente, SIGNAL(cliente_seleccionado(QStringList)), this, SLOT(cargar_cliente(QStringList)));
cliente->show ();
}else inicializar (tipo_factura); //PROBLEM, callls the base class method not looking into the derived one
}
void widgetFacturacion::inicializar (int tipo_factura)
{
qDebug()<<"hi";
ui->setupUi(this);
ui->label_descripcion->clear();
}
All the other virtual methods defined in the derived class are called correctly. Only the one called from the constructor gives me problems.
Sorry if this is too basic.
Thank you all in advance.
My bad, I understood now:
Even though it's a virtual function, the base's version will get called since the derived class isn't fully constructed yet. The base class constructor is called before the derived class constructor, so if the derived virtual function were to get called, it would be with an incompletely-initialized instance - a possible (probably) recipe for disaster.