I had this great idea for controlling data inside the project we have gotten as a school assignement by adding/modifying data inside a global function, but keeping the data containers outside the function.
I quickly learned that this gets quickly limited if u wanna use new inside the functions(I am using pointers). So all the definions and declarations ofc works, but the problem is that as soon as I go outside the function, data dissapears.
Ill give an example of what I wanted to achieve:
I was making a register/login/logout idea, using a list class that my teacher has made for us.
The list system works in the way that if you want use the elements inside the list you have to remove it from the list and copy it over to a element object and then add it back in afterwards.
So, my idea was that register/login/logout was divided in 3 different global functions that was just called in main when certain criterias was met.
When I wanted to register a new user I took in the global user object and the userlist as parameters into the register function, made a new user object(inside the register function), and made sure then the global user object was that new user object(also inside the register function).
The logout function was then to take, again, the global user object and the user list as paramters in, add the user object into the userlist again and go out of the global function.
Login worked by taking the global user object, the userlist, copying each user object from the userlist into the global user object and then comparing data from the userobject with data I asked the user to write in(username, password).
Problem is ofc, that once I go out of these global functions, the data copied/made inside these functions is gone! Dont matter if I take the pointers in with me.
Now, I understand why this isnt possible, but my question is then: Is there a way to do this AND is this a good way of thinking in terms of programming?
I really liked the idea of having a global function working on/with data and then leaving it, atleast for this kind of work. Not saying its a good way of doing everything, but it just seemed so, elegant.. :P
Its not like this is making alot of garbage data that is just left in a void where u cant really find it, its linked to by pointers inside a list.
If I made a member function to the list class so that the member function of the list would create the elements and its data, would it then mean the data wouldnt be deleted once Im outside the global function?
Global data is almost always wrong. The one clear exception to that rule is constants.
Global functions are less frequently wrong. The two clear exception to that rule are functions that operate on generic types (e.g. <algorithms>), and certain operators.
When creating a set of global functions, your first question should be "do these belong in a class?" If the data they operate on belong in a class, it often follows that the functions that operate on that data belong in the same class.
I dont see how your response is valid to what i was asking. :S
I have 3 list classes, 6 element classes and a few more classes. How can I possibly make these into one class without severly making a mess out of the code? :S
The login example takes 1 element and one class, copying/taking out/adding back in the element from the class.
It is no problem in doing this through loops, but I wanted to make the code cleaner by adding the main login/logout/register work of the code into global functions and then calling these in my if/else if loop in main.
Can't really understand what you are trying to achieve with your program, but from what i've understood so far, your main problem is (if i'm not wrong):
-> You have a function that takes in some data
-> and when that function ends, you want that data to be available to other functions
You could go about storing the data in global variables, but i've only been hearing that this is not a good idea.
You should be using the free store, making use of keyword 'new' and some pointer variables. There is no way that data 'disappears', unless you lost track of pointers in your program.
Dont' know if this helps, but if you where to pass data into a function by reference and not by value, then any change to the data wold remain even after the function terminates.
For example:
#include<iostream>
usingnamespace std;
void change_value( int &n) // function that changes the value of the number
{
n = n + 5; // increase the value of n by 5, this should remain
}
int main()
{
int n=0; // the original number
cout<<"The number is "<< n <<endl;
change_value(n); // now we're trying to change the value of n
cout<<"Now the number is "<<n;
return 0;
}
#include<iostream>
usingnamespace std;
void change_value( int *n)
{
*n = *n + 5; // increase the value of n by 5, this should remain
}
int main()
{
int n=0; // the original number
cout<<"The number is "<< n <<endl;
change_value( &n ); // now we're trying to change the value of n
cout<<"Now the number is "<<n;
return 0;
}