How to use variables from a class in an other class?

Hello all,

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?

Gui class header:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#ifndef GUI_H
#define GUI_H
#include "Screen.h"
#include "Variables.h"
#endif


class Gui: public wxFrame
{
	public:
		Gui();
		virtual ~Gui();
	protected:

	private:

		void OnPaint(wxPaintEvent& event);
		
        	Variables var;		// var is a private variable;
        	
		DECLARE_EVENT_TABLE()
};
#endif 

Gui class c++
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#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);
}



Variables class header:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#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 

Variables c++ file:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include "Variables.h"

Variables::Variables()
{
    //initialisatie
    pv1=true;
}

bool Variables::GetPv1()  { return pv1; }

void Variables::setPv1(bool newPv1) { pv1 = newPv1; }

Variables::~Variables()
{
    //dtor
}


The Screen class header:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#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.

Kind regards, Nico
Last edited on
Remove the #endif in line 5 of GUI.h
1
2
3
4
5
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().

Thank you very much for your help.

Kind regards, Nico Nijman
Topic archived. No new replies allowed.