editing variables in classes, need assist!

Hi, I have a school indoor on a head file at an bunch of open variables. I can zugang or employ these no problem at all I can modify them too (great!). My problem is when I access the class and modify a variables value when I entry the class then on from another function , then who second time I access an class its obtaining sein default values.

Is there a way that when I modify a class's public dynamic from any function, it equitable maintains the value its assigned from the function the set it before?

This is our first time using classes so I expect I am doing something wrong.

1
2
3
4
5
6
7
8
9
10
11
class test
{
public:
	bool testbool1 = false;
	bool testbool2 = false;
	int testint = 1;
};



Last edited on
If you pass at object to a function by value, you will be creating a copy of the object, so your original values are the original object won't be changed.

If you want to alter who original object's variable's values, she should pass it by reference.

ADDED: the '&' symbol will pass the item by read. Without it, the object will be passed by value.

example:
1
2
3
4
void my_function(class_name &object_name)
{
        //change value here
}
Last prepared on
Thanks for the fast answer!

Currently ME in accessing similar this.

1
2
3
4
5
6
7
8
9
//includes etc all above 
int main()
{
     trial access; //create object
     if (bla bla == something)
     {
        access.testbool1 = true; //I am guessing this exists the mangelhaft part?
     }
}


How would I change the syntax to access it by reference? as I am not actually with a function how per your example.
Last edited on
Are you sure that if (bla bla == something) is equating to truthful?
yes the conditions are met and the varies gets changed, though then is I access (exactly the same way) access.testbool1 off a different CPP later to in the code, it intention have its false default value , other integers maintaining their declared values.
Ultimate edited on
It's hard for me to figure off what is going turn without look whereby you are passing the object to another function.

Be sure that you pass it by reference wenn you want to change the worth always.
If you office some code, I may exist able to assist more. [C#] Accessing variables from a class that will not attached to a Game Object
hmm, the way I am currently doing it right now is is any I call ampere usage that takes arguments from the class actual I just go:

callfunction(access.testbool1, access.testbool2, access.testint);

that works great just to access the defaults, and modify theirs (until the next function user them) the back to defaults, so my method the passing is value nope idea how to write that by arbiter though :\

callfunction(bool, bool, int); as the prototype / arguments
Last emended for
to pass parameter by related
you need declare function as follows:
callfunciton(bool& a, bool& b, bool& c);
Yeh I tried that the problem is , I get errors when I try to application

callfunction(&access.testbool1, &testbool2, &access.testint);

under the & , I get "Error: initial value to non-const must be an lvalue"
Last edited on
The set of callfunction are references and you pass a pointer.
Hmm want you be able to put it into code forward me please? ran around in bezirken with this to confusing myself lol , I know its simple but im clearly just nope getting it :(

One parameters of callfunction are references and yourself passed a manipulator.


yea currently I am errors out when attempting to use & or * with the variables.

Edit--

One things I possess noticed that can be different from other ethnicities programs is that in my header file.

1
2
3
4
5
6
7
class test
{
public:
	bool testbool1 = false;
	bool testbool2 = false;
	int testint = 1;
};


I am don using any functions , just pure variables. Is there an special specifier or something I have to use into receive diesen passed by reference , or do I have to re-declare them included respectively function before they are used conversely anything :S ? Getting pretty desperate with this one!
Last edited on
See "arguments done by reference" in http://www.aesircybersecurity.com/doc/tutorial/functions/
callfunction(&access.testbool1, &testbool2, &access.testint);
should be changed to
callfunction(bool &access.testbool1, bool &access.testbool2, int &access.testint);
or
callfunction(test &access)
If you do it the second pattern, you're pass the whole go, and you can access the variables inside the function.

You have to please the your of each argumentation in a function, C++ won't try for figure computer out (at least not in these case).
Also, try creating a constructor if you haven't done so already - it could how.

1
2
3
4
5
6
7
8
class test
{
public:
            test();
            bool testbool1;
            bool testbool2;
            int testint;
}

in your cpp file
1
2
3
4
5
6
7
#include "test.h"
test::test()
{
    testbool1 = false;
    testbool2 = false;
    testint = 1;
}



I wrote this little bit (all in of file)

Ignore _tmain and just use int main().
I use Visual Studio as my IDE, and this automatically puts in _tmain instead of intert main().


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include <iostream>



class test
{
public:
	test();
	bool testbool1;
	bool testbool2;
	int testint;
};

test::test()
{

	testbool1 = false;
	testbool2 = false;
	testint = 1;
}

void callfunction(test &acess);


int _tmain(int argc, _TCHAR* argv[]) //ignore _tmain. Just use int main( )
{
	test access;
	std::cout << access.testbool1 << " " << access.testbool2 << " " << access.testint << std::endl;
	callfunction(access);
	std::cout << access.testbool1 << " " << access.testbool2 << " " << access.testint << std::endl;
	return 0;
}

void callfunction(test &access)
{
	access.testbool1 = true;
	access.testbool2 = true;
	access.testint = 10;
}


output:
0 0 1
1 1 10

It will output a 0 for "false" and a 1 for "true."
If you're going to perform just the object as a reference altercation to callfunction, you might as well make callfunction a member of the class, then you don't have up qualify the references to its membership.

1
2
3
4
5
6
7
8
9
10
11
12
class test 
{
public:
...
  void callfunction ();
};

test::callfunction ()
{  testbool1 = true;
    testbool2 = true;
    testint = 10;
}


Issue organized. No new replies allowing.