editing variables in classes, need help!

Nov 14, 2014 at 4:44am
Hi, I have a class inside of a header file with a bunch of public variables. I can access and use 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 access the class later on from another function , then the second time I access the class its obtaining its default values.

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

This is my 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 Nov 14, 2014 at 4:49am
Nov 14, 2014 at 4:50am
If you pass an object to a function by value, you will be creating a copy of the object, so your original values in the original object won't be changed.

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

ADDED: the '&' symbol will pass the object by reference. 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 edited on Nov 14, 2014 at 4:52am
Nov 14, 2014 at 5:04am
Thanks for the fast reply!

Currently I am accessing like this.

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


How would I change the syntax to access it by reference? as I am not actually using a function as per your example.
Last edited on Nov 14, 2014 at 5:06am
Nov 14, 2014 at 5:07am
Are you sure that if (bla bla == something) is equating to true?
Nov 14, 2014 at 5:09am
yes the conditions are met and the variable gets changed, but then if I access (exactly the same way) access.testbool1 from a different CPP later on in the code, it will have its false default value , or integers maintaining their declared values.
Last edited on Nov 14, 2014 at 6:01am
Nov 14, 2014 at 6:20am
It's hard for me to figure out what is going on without seeing how you are passing the object to another function.

Be sure that you pass it by reference if you want to change the value permanently.
If you post some code, I may be able to help more.
Nov 14, 2014 at 7:01am
hmm, the way I am currently doing it right now is that anytime I call a function that takes arguments from the class variables I just go:

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

that works great just to access the defaults, and modify them (until the next function uses them) and back to defaults, so my method of passing is value no idea how to write that by ref though :\

callfunction(bool, bool, int); as the prototype / arguments
Last edited on Nov 14, 2014 at 7:05am
Nov 14, 2014 at 7:20am
to pass parameter by ref
you need declare function as follows:
callfunciton(bool& a, bool& b, bool& c);
Nov 14, 2014 at 7:30am
Yeh I tried that the problem is , I get errors if I try to use

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

under the & , I get "Error: initial value to non-const must be an lvalue"
Last edited on Nov 14, 2014 at 7:30am
Nov 14, 2014 at 7:44am
The parameters of callfunction are references and you pass a pointer.
Nov 14, 2014 at 12:30pm
Hmm would you be able to put it into code for me please? ran around in circles with this to confusing myself lol , I know its simple but im clearly just not getting it :(

The parameters of callfunction are references and you pass a pointer.


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

Edit--

One thing I have noticed that could be different from other peoples 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 not using any functions , just pure variables. Is there a special specifier or something I have to use to get these passed by reference , or do I have to re-declare them in each function before they are used or something :S ? Getting pretty desperate with this one!
Last edited on Nov 14, 2014 at 12:57pm
Nov 14, 2014 at 1:19pm
See "arguments passed by reference" in http://www.cplusplus.com/doc/tutorial/functions/
Nov 14, 2014 at 1:26pm
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 way, you're passing the whole object, and you can access the variables inside the function.

You have to specify the type of each argument in a function, C++ won't try to figure it out (at least not in this case).
Also, try creating a constructor if you haven't done so already - it could help.

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 one file)

Ignore _tmain and just use int main().
I use Visual Studio as my IDE, and it automatically puts in _tmain instead of int 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."
Nov 14, 2014 at 2:15pm
If you're going to pass just the object as a reference argument to callfunction, you might as well make callfunction a member of the class, then you don't have to qualify the references to its members.

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;
}


Topic archived. No new replies allowed.