Ok so i am working on pointers and its going well but referencing stuff in the constructors can get hectic, especially when i have 7 different things that need to be referewnced. Im looking for a way to reference all this stuff without using classes, making them global or traditional referencing. What i was thinking of is this:
Ok so lets say i have 5 variables: int a, int b, int c, string d, string e.
Now instead if putting them all in my constructors of the functions i want to use i was thinking of something like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
void function(int *hold)
int main()
{
int a = 12;
int b = 13;
int c = 56;
int d = 67;
int e = 1;
int hold;
hold = function(int &a, int &b, int &c, int &d, int &e);
}
void function(int *hold)
{
cout << *hold << endl;
}
So basically i want to be able to reference my variables withou having to type in all of them in my function constructors, can something like this even work?
No. You have to pass in the parameters the function expects. In this case, a pointer to an integer.
Moreover, there are a couple of other errors there. Namely that you wouldn't include data types when calling a function and that you're trying to assign a void function's return value (or lack thereof) to a variable.
Out of curiosity, why wouldn't you just use a class, struct or array?
and if you want to pass a variable by reference to a function, something like this example will work:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
int add(int&,int&);//function prototype
int main()
{
int x,y;
add(x,y);
cout<<"Ans: "<<x+y;
cin.ignore();
cin.get();
}
int add(int& a,int& b)
{
cout<<"Enter a number: ";
cin>>x;
cout<<"Enter another one to add to the first number: ";
cin>>y;
}