Hi,

I created a simple class that should provide Indexnumbers for a Node-Editor.

indexhandler.h:
Qt Code:
  1. #ifndef INDEXHANDLER_H
  2. #define INDEXHANDLER_H
  3.  
  4. enum type {NODE=0 ,PIN=1 , WIRE=2};
  5.  
  6. class Indexhandler
  7. {
  8. public:
  9. Indexhandler();
  10.  
  11. int getNodeindex() const;
  12. void setNodeindex(int value);
  13.  
  14. int getPinindex() const;
  15. void setPinindex(int value);
  16.  
  17. int getWireindex() const;
  18. void setWireindex(int value);
  19.  
  20. int requestIndex(int type);
  21.  
  22. private:
  23. int nodeindex;
  24. int pinindex;
  25. int wireindex;
  26. };
  27.  
  28. #endif // INDEXHANDLER_H
To copy to clipboard, switch view to plain text mode 

indexhandler.cpp:
Qt Code:
  1. #include "indexhandler.h"
  2.  
  3. Indexhandler::Indexhandler()
  4. {
  5. setPinindex(0);
  6. setNodeindex(0);
  7. setWireindex(0);
  8. }
  9.  
  10. int Indexhandler::getNodeindex() const
  11. {
  12. return nodeindex;
  13. }
  14.  
  15. void Indexhandler::setNodeindex(int value)
  16. {
  17. nodeindex = value;
  18. }
  19. int Indexhandler::getPinindex() const
  20. {
  21. return pinindex;
  22. }
  23.  
  24. void Indexhandler::setPinindex(int value)
  25. {
  26. pinindex = value;
  27. }
  28. int Indexhandler::getWireindex() const
  29. {
  30. return wireindex;
  31. }
  32.  
  33. void Indexhandler::setWireindex(int value)
  34. {
  35. wireindex = value;
  36. }
  37.  
  38. int Indexhandler::requestIndex(int type)
  39. {
  40. switch(type)
  41. {
  42. case PIN: return getPinindex();
  43. case NODE: return getNodeindex();
  44. case WIRE: return getWireindex();
  45. default: return -1;
  46. }
  47.  
  48. }
To copy to clipboard, switch view to plain text mode 

I instantiate it in the MainWindows constructor like this:
Qt Code:
  1. #include "mainwindow.h"
  2. #include "ui_mainwindow.h"
  3. #include <QDebug>
  4.  
  5. MainWindow::MainWindow(QWidget *parent) :
  6. QMainWindow(parent),
  7. ui(new Ui::MainWindow)
  8. {
  9. ui->setupUi(this);
  10.  
  11. Indexhandler *indexhandler = new Indexhandler;
  12.  
  13. qDebug() << indexhandler->getNodeindex();
  14. }
  15.  
  16. MainWindow::~MainWindow()
  17. {
  18. delete ui;
  19. }
  20.  
  21. void MainWindow::on_pushButton_clicked()
  22. {
  23. qDebug() << indexhandler->getNodeindex();
  24. }
To copy to clipboard, switch view to plain text mode 

The first qDebug() prints out 0 as expected,
but the second one (after the pushButton_clicked() returns a random number. Since this number only changes if the Program is restart it looks like a Stray Pointer to me.
What am I doing wrong?