vendredi 15 octobre 2021

Empty constructor doesn't get executed

I'm trying to create a class with 2 constructors. An empty one where I initialize each member to a default value, and a constructor with parameters where I initialize each member to those parameters:

Sesion.h

class Sesion
{
  public:
    Sesion();
    Sesion(int idUser, int codUser, int idVehicle, int codVehicle);

    Sesion& operator= (const Sesion& sesion);


    //Getters
    int getIdSesion() const;

    int getIdUser() const;

    int getCodUser() const;

    int getIdVehicle() const;

    int getCodVehicle() const;

    //Setters
    void setIdSesion(int idSesion);
    
  private:
    int idSesion;
    int idUser;
    int codUser;
    int idVehicle;
    int codVehicle;   
};

Sesion.cpp

Sesion::Sesion()
{
  cout << "Empty constructor called" << endl;
  idSesion = -1;
  idUser= -1;
  codUser = -1;
  idVehicle = -1;
  codVehicle = -1;
}

Sesion::Sesion(int idUser, int codUser, int idVehicle, int codVehicle)
{
  this->idSesion = -1;
  this->idUser = idUser;
  this->codUser = codUser;
  this->idVehicle = idVehicle;
  this->codVehicle = codVehicle;
}

int Sesion::getIdSesion() const
{
  return idSesion;
}
...

So when I try to execute this in another part of the program:

Sesion sesion;
sesion.getIdSesion();

The value of IdSesion is 1994327248 when it should be -1. If I execute the program several times I get different values, but the 1994 at the beginning stays the same.

I know the empty constructor isn't executing because the cout never prints anything so, what constructor is getting called when I create the object?

I am compiling this program for two different platforms. On the first one I use Qt 4.8.6 and in the other one I use Qt 5.2.1.

On the first platform the program doesn't behave correctly, as I just explained. On the second platform the empty constructor gets called correctly and the value of IdSesion is -1.

I don't have right now the information about what C++ standards I'm using to compile for each platform, but I know I'm using a different one for each of them.

The only thing I can say is that on the first platform I can't use foreach loops while in the other platform I can, so maybe you know which standards I'm probably using.

Also the main problem is that I am only able to use a debugger on the second platform, where it works correctly. On the first platform I don't have a debugger available so I'm "blind" and I can only include print statements to follow the code when I execute it.

Aucun commentaire:

Enregistrer un commentaire