Created
October 9, 2011 00:38
-
-
Save 7shi/1273115 to your computer and use it in GitHub Desktop.
メンバ・非メンバの関数ポインタを統合的に扱う
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <functional> | |
#include <stdio.h> | |
struct Test1 { | |
int n; | |
Test1(int n) : n(n) {} | |
void show() { | |
printf("%d\n", n); | |
} | |
}; | |
struct Test2 { | |
int n; | |
Test2(int n) : n(n) {} | |
void show() { | |
printf("%d\n", n); | |
} | |
}; | |
struct Test3 { | |
static void show() { | |
printf("?\n"); | |
} | |
}; | |
void show() { | |
printf("!\n"); | |
} | |
void call(const std::function<void()> &f) { | |
f(); | |
} | |
int main() { | |
Test1 t1(1); | |
Test2 t2(2); | |
auto f1 = std::bind(&Test1::show, &t1); | |
auto f2 = [&] { t2.show(); }; | |
auto f3 = Test3::show; | |
auto f4 = show; | |
call(f1); | |
call(f2); | |
call(f3); | |
call(f4); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
type Test1(n) = | |
member x.show() = printfn "%d" n | |
type Test2(n) = | |
member x.show() = printfn "%d" n | |
type Test3 = | |
static member show() = printfn "?" | |
let show() = printfn "!" | |
let call f = f() | |
let t1 = new Test1(1) | |
let t2 = new Test2(2) | |
let d1 = t1.show | |
let d2 = fun() -> t2.show() | |
let d3 = Test3.show | |
let d4 = show | |
call d1 | |
call d2 | |
call d3 | |
call d4 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment