Sorry for the bad title, I didn't know what to name my topic.
Anyway... I want to know if it's possible (and how it's possible) to send an input to a function without declaring it in a temporary variable?
Messy question... let's make an example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
#include <iostream>
usingnamespace std;
void myFunc(char input)
{
// Do something...
}
int main()
{
char input; // I want to get rid of this temporary variable
cin >> input; // and send wathever input directly to my function.
myFunc(input);
}
example:
1 2 3 4
int main()
{
myfunc(input = cin/getline/whatever); // This is just an example
}
If you need more information about what I want to accomplish, please let me know and I will try to clarify my question even more.
I don't think you can, because of the typing of the variables. cin << needs to know what kind of variable it is extracting the data to, so it knows how much memory is available for it.
Possibly you can define a template function object and overload the << operator in that, but that would take more coding than you would just write the simple variable declaration probably.
As for you're 2nd post:
1 2 3 4
int main()
{
myFunc2 ( 5.5*2.5 ); //this does the same
}
It's not about how short your code is - it's about how optimal it works the memory (and cpu) and how readable it is.
@NwN
Absolutely, your example does exactly the same as mine. And it's 'cheaper'. But the question was if it possible to combine the functions as they are, not it's content.
I guess it's possible, since you didn't tell me otherwhise?
The conclusion brings me another question... Is the memory needed for the temporary variable for the cin >> data discharged after it's been used, or do I need to put an extra line of code to restore the used memory?
I hope I'm not confusing anyone, I'm a bit confused myself =)
The memory for char input; is released at the end of the function.
In this case it's main() - so at the end of the program.
To my knowledge, you can only free dynamically allocated memory:
http://cplusplus.com/doc/tutorial/dynamic/
Yes, combining the functions as such is possible, the inner function will be evaluated first and its return value will then be used by the outer function.
You cannot remove any variable/object from the stack whenever you wish, because that'll violate the concept of the stack. To overcome this, programmers create an anonymous scope to discard temporary variables/objects. In a way, it's controlling the lifetime of a variable/object.
1 2 3 4 5 6 7 8 9 10 11
int main()
{
{
// We're inside the anonymous scope.
char temp(0);
std::cin >> temp;
myFunc(temp);
}
// 'temp' is no longer on the stack; it's non-existent.
}
All you need is an input function that will return the entered value. Standard C++ stream input functions usually return a reference to a stream. However some C functions as for example getchar return the entered value. So you can write
1 2 3 4 5 6 7 8 9 10 11
#include <cstdio>
void myFunc(char input)
{
// Do something...
}
int main()
{
myFunc( std::getchar() );
}
By the way you could use a C++ feature. myFunc( std::cin.get() );
In what way is this different the first example I posted? Everyone told me this was inoperable... myfunc(input = cin/getline/whatever); // This is just an example