-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunction_ptr.cpp
79 lines (62 loc) · 1.32 KB
/
function_ptr.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
//
// Created by srigun on 8/8/20.
//
#include <iostream>
#include <string>
#include <functional>
#include <map>
// Define fp
template<typename T>
using fcn = std::function<T()>;
using fcn2 = std::string();
//template <typename T>
class A {
public:
int getInt(){
return 0;
}
std::string getString(){
std::string ret_str = "asd";
return ret_str;
}
template<typename T>
std::function<T()> return_function_ptr()
{
return this.get<T>;
}
template<typename T>
void get(){
}
template<>
std::string get<std::string>(){
return "hello";
}
template<>
int get<int>(){
return 100;
}
};
class B : public A {
public:
template<typename T>
std::function<T()> getFunctor() {
//fcn<std::string> s{this->getString()};
//fcn<std::string> i{&getStringOutside};
auto ret = A::return_function_ptr<T>();
return ret;
}
};
int main()
{
B b;
auto fctr_string = b.getFunctor<std::string>();
auto str_val = fctr_string();
auto fctr_int = b.getFunctor<std::string>();
auto int_val = fctr_int();
std::cout << "str_val = " << str_val << "\n";
std::cout << "int_val = " << int_val << "\n";
const int *p_int;
int y = 10;
p_int = &y;
std::cout << std::endl;
}