So I was going through some notes and I got confused by some weird declarations. In the code segment below, you can see the below chunk of code has a function which is assigned to an auto variable
#include<iostream>
using namespace std;
void Hi(int a)
{
cout << "Hi" << a << endl;
}
int main()
{
auto Print = Hi;
Print(5);
}
But the function ('Hi' here) doesnt have brackets () and it still works..so how does it work? What ticks me more is what is its type? And at the end when we call that variable it prints 'Hi5'.
I was curious on this part because this was just an example part of our code. We needed to make a working example of a for-each loop function to display values in a vector using such a type.
I cant seem to find any lead on it as I dont even know the type..but our professor did mention to use 'lambdas' for the better, which I looked up in the net but I couldnt get a clear idea of what it is. Any heads up would be really helpful.
Aucun commentaire:
Enregistrer un commentaire