Skip to content

Instantly share code, notes, and snippets.

@krysseltillada
Created June 29, 2015 15:19
Show Gist options
  • Save krysseltillada/4303f67e7fef5c4dd34f to your computer and use it in GitHub Desktop.
Save krysseltillada/4303f67e7fef5c4dd34f to your computer and use it in GitHub Desktop.
function pointers exercise3
#include <iostream>
/// function pointer
int sum (int i, int ii)
{
return i + ii;
}
int (*ptr_f)(int, int) = sum; /// ptr_f is a function pointer that have a parameters of two int and returns int
/// function pointer parameter
int f_mimic1 (int i, int ii,int (*f_ptr) (int, int))
{
return f_ptr(i, ii);
}
/// function pointer parameter with type alias typedef
typedef int (*ptr_tdf1)(int, int);
int f_mimic2 (int i, int ii, ptr_tdf1 f1)
{
return f1 (i, ii);
}
typedef int ptr_tdf2 (int, int);
int f_mimic3 (int i, int ii, ptr_tdf2 *f2)
{
return f2(i, ii);
}
/// function pointer parameter with typedef and decltype
typedef decltype(sum) *td_f;
int f_mimic4 (int i, int ii, td_f f3)
{
return f3(i, ii);
}
typedef decltype(sum) td_f2;
int f_mimic5 (int i, int ii, td_f2 f4)
{
return f4 (i, ii);
}
/// overloaded function pointers ///
int sample_ovf (double d1, double d2)
{
std::cout << __func__ << " double is called " << std::endl;
return d1 * d2;
}
int sample_ovf (int i1, int i2)
{
std::cout << __func__ << " int is called " << std::endl;
return i1 * i2;
}
int (*ptr_sample_ovf) (double, double) = sample_ovf;
/// ///////////////////////////////
/// return a function pointer ///
std::string::size_type s_r (std::string str1, std::string str2)
{
return str1.size() + str2.size();
}
std::string::size_type (*f_r (std::string::size_type (*p_str_f) (std::string, std::string))) (std::string, std::string)
{
return p_str_f;
}
/// /////////////////////////////
/// return a function pointer using type alias typedef
typedef std::string::size_type (*t_f2)(std::string, std::string);
t_f2 tpdf (std::string::size_type (*p_str_f)(std::string, std::string))
{
return p_str_f;
}
/// return a function pointer using typedef and decltype
std::string::size_type sample_f (std::string, std::string);
typedef decltype(sample_f) *td_f3;
td_f3 td_f1 (std::string::size_type (*p_str1_f) (std::string, std::string))
{
return p_str1_f;
}
/// ////////////////////////////////////////
/// return a function pointer using trailing return type
auto tr_f (std::string::size_type (*p_str2_f) (std::string, std::string)) -> std::string::size_type (*) (std::string,
std::string)
{
return p_str2_f;
}
/// ////////////////////////////////////////
int main()
{
std::string str1 = "sample", str2 = "example";
std::cout << sum (12, 12) << std::endl
<< ptr_f (200, 2440) << std::endl;
std::cout << (*ptr_f) (22,22) << std::endl;
std::cout << f_mimic1 (1, 2, sum) << std::endl
<< f_mimic2 (1, 6, sum) << std::endl
<< f_mimic3 (22, 22, sum) << std::endl
<< f_mimic4 (100, 100, sum) << std::endl
<< f_mimic5 (100, 223, sum) << std::endl;
std::cout << sample_ovf (2, 2) << std::endl
<< ptr_sample_ovf (5, 5) << std::endl;
std::cout << (*ptr_sample_ovf) (10, 10) << std::endl;
std::string::size_type (*str_r) (std::string, std::string) = f_r(s_r);
std::cout << str_r (str1, str2) << std::endl;
std::cout << (*str_r) (str1, str2) << std::endl;
std::string::size_type (*str_r2) (std::string, std::string) = tpdf(s_r);
std::cout << str_r2 (str1, str2) << std::endl;
std::cout << (*str_r2) (str1, str2) << std::endl;
std::string::size_type (*str_r3) (std::string, std::string) = td_f1(s_r);
std::cout << str_r3 (str1, str2) << std::endl;
std::cout << (*str_r3) (str1, str2) << std::endl;
std::string::size_type (*str_r4) (std::string, std::string) = tr_f(s_r);
std::cout << str_r4 (str1, str2) << std::endl;
std::cout << (*str_r4) (str1, str2) << std::endl;
std::string (*ptr_f)[10] = str_fr({"hello", "world", "test"});
for (unsigned it = 0; it != 10; ++it)
std::cout << (*ptr_f)[it] << std::endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment