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;
};
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
}
//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.
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.
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
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!
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;
}
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.