Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
member function pointer
Language:
Ada
Assembly
Bash
C#
C++ (gcc)
C++ (clang)
C++ (vc++)
C (gcc)
C (clang)
C (vc)
Client Side
Clojure
Common Lisp
D
Elixir
Erlang
F#
Fortran
Go
Haskell
Java
Javascript
Kotlin
Lua
MySql
Node.js
Ocaml
Octave
Objective-C
Oracle
Pascal
Perl
Php
PostgreSQL
Prolog
Python
Python 3
R
Rust
Ruby
Scala
Scheme
Sql Server
Swift
Tcl
Visual Basic
Layout:
Vertical
Horizontal
//g++ 5.4.0 #include <iostream> using namespace std; class Test; typedef void (Test::*Func)(); // member function pointer typedef void (*FuncPtr)(); // function pointer class Test{ string name; Func f; // declare member function pointer public: Test(const char* name) :name(name), f(&Test::func1) // define member function pointer {} // member funtion void func1(){ cout<<name <<" call func1\n";} void func2(){ cout<<name <<" call func2\n";} void func(){ (this->*f)(); // call member function pointer } }; // function void func1(){ cout<<"func1\n";} void func2(){ cout<<"func2\n";} int main() { Func f = &Test::func2; // define member function pointer FuncPtr fp = func1; // define function pointer Test a("a"), b("b"); (a.*f)(); // use member function pointer b.func(); // use member function fp(); // use function pointer }
g++
Show compiler warnings
[
+
] Compiler args
[
+
]
Show input
Compilation time: 0.43 sec, absolute running time: 0.18 sec, cpu time: 0.11 sec, memory peak: 3 Mb, absolute service time: 0,61 sec
edit mode
|
history
|
discussion
a call func2 b call func1 func1