PDA

View Full Version : Inheritance problem {SOLVED}



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.

wysota
24th July 2013, 07:23
At the time of construction of the base class, the derived class is not yet created thus the virtual method table is not overwritten with methods redefined in the derived class thus calling virtual methods from the constructor of the base class cannot access any code from the derived class. Trying to do that results in the code from the base class being invoked and is considered a programming error as far as the standards go.

KillGabio
24th July 2013, 08:21
Thank you, I already editted my answer sorry for wasting your time. I was reading but not finding any answer till I camed across with a FAQ of C++ lol.

Added after 22 minutes:

I have another question that is bugging me, regarding this two classes. When I hit a button to close the derived Widget. The function called by the click of the button is executed twice, any ideas why this can be happening??


In fact every button that closes the widget is called twice :/

Added after 19 minutes:

Got it, I was declaring the slots again in the derived class.
protected slots: That line is wrong in derived.h

wysota
24th July 2013, 08:58
You should name your slots differently (declaring what they do and not when they are invoked) and use connect() statements to connect them to signals.