I have the following code to demonstrate a function been called inside another function.
The below code works correctly:
#include <iostream>
int thirds()
{
return 6 + 1;
}
template <typename T, typename B>
int hello(T x, B y , int (*ptr)() ){
int first = x + 1;
int second = y + 1;
int third = (*ptr) (); ;
return first + second + third;
}
int add(){
int (*ptr)() = &thirds;
return hello(1,1, thirds);
}
int main()
{
std::cout<<add();
return 0;
}
Now I want to pass one number as a parameter from add function ie into thirds function (thirds(6)).
I am trying this way:
#include <iostream>
int thirds(int a){
return a + 1;
}
template <typename T, typename B>
int hello(T x, B y , int (*ptr)(int a)() ){
int first = x + 1;
int second = y + 1;
int third = (*ptr)(a) (); ;
return first + second + third;
}
int add(){
int (*ptr)() = &thirds;
return hello(1,1, thirds(6)); //from here pass a number
}
int main()
{
std::cout<<add();
return 0;
}
My expected output is:
11
But It is not working. Please can someone show me what I am doing wrong?
Aucun commentaire:
Enregistrer un commentaire