I am trying to use and change the values of a variable in other classes. I'd think this should not be to difficult, but I don't get it to work as I would like. Below are the header files and c++ files of three classes. The Variables class should hold the value of the variable. It is supposed to be created in the Gui class where the value of the variable is also changed. Next its instance should be passed to the Screen class where the value of the variable is used.
However the output I get from the Screen class indicates the variable "pv1" is the initial "true". This could mean I accidentally created a new instance of the Variables class or that the variable was not changed with the call in the Gui class. Is there anybody that can help me get this construction to work?
#include "Gui.h"
#include "Screen.h"
#include "Variables.h"
BEGIN_EVENT_TABLE(Gui, wxFrame)
EVT_PAINT(Gui::OnPaint) // this is part of wxWidgets. it makes sure the OnPaint methode is called if the screen size changes. I don't know how but it works
END_EVENT_TABLE()
Gui::Gui()
{
Variables var; // here I create the class with the variables
var.setPv1(false); // imidately after I try to set a variable to false (this variable is set to true by default)
}
void Gui::OnPaint()
{
Screen screen; // when the screen is resized or created I craete a class to build the screen and I call is function to draw the screen.
// the arguments are: dc a wxWidgets thing to draw on, the width and the hight of the screen, a horizontal and a verticle offset
// and most important for my question: the variable that I hope represents the class that is holding my variables
screen.drawScreen(dc, breedte, hoogte, hOffset, vOffset, var);
}
#ifndef VARIABLES_H
#define VARIABLES_H
class Variables
{
public:
bool pv1; // it would be nicer if this could be a private variable.
Variables();
virtual ~Variables();
//bool getPV1(); // not yet implemented, this is how i hope to change pv1 to private
void setPv1(bool newPv1);
protected:
private:
};
#endif
#ifndef SCREEN_H
#define SCREEN_H
#include "Variables.h"
class Screen
{
public:
Screen();
void drawScreen(wxDC& dc, int breedte, int hoogte, int hOffset, int vOffset, Variables var);
virtual ~Screen();
protected:
private:
};
#endif
and finally the Screen c++ file:
1 2 3 4 5 6 7 8 9 10 11
#include "Screen.h"
#include "Variables.h"
Screen::Screen() {}
void Screen::drawScreen(wxDC& dc, int breedte, int hoogte, int hOffset, int vOffset, Variables var)
{
if (var.pv1){ dc.SetBrush(vulGroen); dc.SetPen(stikstof); } // select a color depending on the value of the variable
else { dc.SetBrush(vulZwart); dc.SetPen(kader); }
dc.drawline(0,0,breedte,hoogte); // draw a diagonal line in the window
}
In these files I left out some overhead for using wxWidgets as they have nothing to do with the passing of variables.
I hope someone is able to tell me where I went wrong.
Gui::Gui()
{
Variables var; // here you create a local variable
var.setPv1(false); // here you modify a local variable
} //here the local variable dies
Also having setter and getters that "do nothing" is even worse than a public variable
Thank you for your reply. I agree the code is a bit messy at this moment and you are absolutely right about your getter/setter remark. This is just a result of me trying to find my problem while apparently looking in the wrong place.
Thanks to your reply I found I should remove Variables var; in Gui. By not creating a new local variable there the same variable "var" is used in Gui::Gui() and Gui::OnPaint().