Hi,
I created a simple class that should provide Indexnumbers for a Node-Editor.
indexhandler.h:
#ifndef INDEXHANDLER_H
#define INDEXHANDLER_H
enum type {NODE=0 ,PIN=1 , WIRE=2};
class Indexhandler
{
public:
Indexhandler();
int getNodeindex() const;
void setNodeindex(int value);
int getPinindex() const;
void setPinindex(int value);
int getWireindex() const;
void setWireindex(int value);
int requestIndex(int type);
private:
int nodeindex;
int pinindex;
int wireindex;
};
#endif // INDEXHANDLER_H
#ifndef INDEXHANDLER_H
#define INDEXHANDLER_H
enum type {NODE=0 ,PIN=1 , WIRE=2};
class Indexhandler
{
public:
Indexhandler();
int getNodeindex() const;
void setNodeindex(int value);
int getPinindex() const;
void setPinindex(int value);
int getWireindex() const;
void setWireindex(int value);
int requestIndex(int type);
private:
int nodeindex;
int pinindex;
int wireindex;
};
#endif // INDEXHANDLER_H
To copy to clipboard, switch view to plain text mode
indexhandler.cpp:
#include "indexhandler.h"
Indexhandler::Indexhandler()
{
setPinindex(0);
setNodeindex(0);
setWireindex(0);
}
int Indexhandler::getNodeindex() const
{
return nodeindex;
}
void Indexhandler::setNodeindex(int value)
{
nodeindex = value;
}
int Indexhandler::getPinindex() const
{
return pinindex;
}
void Indexhandler::setPinindex(int value)
{
pinindex = value;
}
int Indexhandler::getWireindex() const
{
return wireindex;
}
void Indexhandler::setWireindex(int value)
{
wireindex = value;
}
int Indexhandler::requestIndex(int type)
{
switch(type)
{
case PIN: return getPinindex();
case NODE: return getNodeindex();
case WIRE: return getWireindex();
default: return -1;
}
}
#include "indexhandler.h"
Indexhandler::Indexhandler()
{
setPinindex(0);
setNodeindex(0);
setWireindex(0);
}
int Indexhandler::getNodeindex() const
{
return nodeindex;
}
void Indexhandler::setNodeindex(int value)
{
nodeindex = value;
}
int Indexhandler::getPinindex() const
{
return pinindex;
}
void Indexhandler::setPinindex(int value)
{
pinindex = value;
}
int Indexhandler::getWireindex() const
{
return wireindex;
}
void Indexhandler::setWireindex(int value)
{
wireindex = value;
}
int Indexhandler::requestIndex(int type)
{
switch(type)
{
case PIN: return getPinindex();
case NODE: return getNodeindex();
case WIRE: return getWireindex();
default: return -1;
}
}
To copy to clipboard, switch view to plain text mode
I instantiate it in the MainWindows constructor like this:
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDebug>
MainWindow
::MainWindow(QWidget *parent
) : ui(new Ui::MainWindow)
{
ui->setupUi(this);
Indexhandler *indexhandler = new Indexhandler;
qDebug() << indexhandler->getNodeindex();
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_pushButton_clicked()
{
qDebug() << indexhandler->getNodeindex();
}
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDebug>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
Indexhandler *indexhandler = new Indexhandler;
qDebug() << indexhandler->getNodeindex();
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_pushButton_clicked()
{
qDebug() << indexhandler->getNodeindex();
}
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?
Bookmarks