KH_C++
ํจ์ ํฌ์ธํฐ(Function Pointer) ๋ณธ๋ฌธ
๐ ๋ฐ์ดํฐ ํฌ์ธํฐ
๋ฐ์ดํฐ ํฌ์ธํฐ๋ ๋ณ์์ ์ฃผ์๋ฅผ ์ ์ฅํ๊ณ ์๋ค๊ฐ ํด๋น ์ฃผ์์ ๊ฐ์ ์ฝ์ด์ค๊ฑฐ๋ ๋ณ๊ฒฝํ ๋ ์ฌ์ฉํ๋ ๋ฌธ๋ฒ์ ๋๋ค.
์๋ฅผ ๋ค์ด int ์๋ฃํ์ ๊ฐ์ง data๋ผ๋ ๋ณ์๊ฐ ์๋ค๋ฉด data ๋ณ์ ์ด๋ฆ์ ์ฌ์ฉํ์ฌ ์ง์ ๊ฐ์ 5๋ก ๋ณ๊ฒฝํ ์์์ต๋๋ค. ๊ทธ๋ฆฌ๊ณ ํฌ์ธํฐ ๋ณ์ p๋ฅผ ์ ์ธํด์ '๋ฒ์ง ๊ณ์ฐ ์ฐ์ฐ์('&')๋ฅผ ์ฌ์ฉํด์ data ๋ณ์ ์ฃผ์๋ฅผ ํฌ์ธํฐ ๋ณ์ p์ ์ ์ฅํ ๋ค์ ํฌ์ธํฐ ๋ณ์ p์ '๋ฒ์ง ์ง์ ์ฐ์ฐ์('๏ผ')๋ฅผ ์ฌ์ฉํ์ฌ ๊ฐ์ ์ ์ผ๋ก data ๋ณ์์ ๊ฐ์ 6์ผ๋ก ๋ณ๊ฒฝํ ์๋ ์๋ค.
๐ ํจ์ ํฌ์ธํฐ
๊ทธ๋ฐ๋ฐ ํฌ์ธํฐ๊ฐ ๋ฐ์ดํฐ(๋ณ์)์ ์ฃผ์๋ง ์ ์ฅํ๋ค๊ฐ ์ฌ์ฉํ ์ ์๋ ๊ฒ์ ์๋๋ค. c ์ธ์ด์์ ํจ์๋ผ๊ณ ๋ถ๋ฆฌ๋ ๋ช ๋ น์ด์ ์งํฉ์ฒด๋ ์ปดํ์ผ๋๋ฉด ๊ธฐ๊ณ์ด๋ก ๋ณ๊ฒฝ๋๊ณ ํ๋ก๊ทธ๋จ์ด ์คํ๋๋ฉด ์ฝ๋ ์ธ๊ทธ๋จผํฐ๋ผ๋ ๋ฉ๋ชจ๋ฆฌ ์์ญ์ ์์นํ๊ฒ ๋๋ค. ์ฆ, ํจ์์ ํํ๋ ๋ณ๊ฒฝ๋๊ฒ ์ง๋ง ๊ฒฐ๊ตญ ๋ฉ๋ชจ๋ฆฌ์ ์ ์ฅ๋๊ธฐ ๋๋ฌธ์ ์ฃผ์๋ฅผ ๊ฐ์ง๊ฒ ๋๋ค.
ํจ์ ํฌ์ธํฐ(Function Pointer)๋ ํจ์์ ์ฃผ์๋ฅผ ์ ์ฅํ๋ค๊ฐ ํด๋น ์ฃผ์์ ํจ์๋ฅผ ํธ์ถํ๋๋ฐ ์ฌ์ฉํ๋ ํฌ์ธํฐ๋ฅผ ๋ปํ๋ค.
๐ ํจ์ ํฌ์ธํฐ ์ ์ ๋ฐฉ๋ฒ
1
2
3
4
5
6
7
8
9
10
11
12
13
|
int func(int x)
{
return x;
}
int main()
{
cout << func << endl; // 01201084 ์ถ๋ ฅ (ํจ์์ ์ฃผ์)
int (*funcptr)(int) = func; // ํจ์ ํฌ์ธํฐ funcptr ์ ์ธ ๋ฐ ์ด๊ธฐํ
cout << funcptr << endl; // 01201084 ์ถ๋ ฅ
}
|
๐ int : ์ฃผ์์ ์๋ ํจ์์ ๋ฆฌํด ํ์
๐ (*funcptr) : ํจ์ ํฌ์ธํฐ ์ด๋ฆ funcptr
๐ (int) : ์ฃผ์์ ์๋ ํจ์์ ๋งค๊ฐ๋ณ์ ํ์
์ ์ด์ฃผ๋ ๋ถ๋ถ. ์์ผ๋ฉด ๊ทธ๋ฅ ()
๐ = func : func ํจ์์ ์ฃผ์๋ก ์ด๊ธฐํ ํจ. ํจ์์ ์ด๋ฆ์ ๊ทธ ํจ์์ ์ฃผ์ !
๐ ํจ์ ํฌ์ธํฐ ์ฌ์ฉ ๋ฐฉ๋ฒ
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
int func(int x)
{
return x;
}
int main()
{
cout << func << endl;
int (*funcptr)(int) = func;
cout << funcptr << endl;
cout << funcptr(5) << endl;
// 5์ถ๋ ฅ. func ํจ์์ ์ฃผ์๋ฅผ ๊ฐ์ง๊ณ ์๋ ํฌ์ธํฐ funcptr๋ก fun ํจ์๋ฅผ ์ฌ์ฉํ ์ ์๊ฒ ๋์์
}
|
๐ํจ์ ํฌ์ธํฐ ์ด๋ฆ(ํ๋ผ๋ฏธํฐ)
1. ์ด๋ ๊ฒ ์ฃผ์๋ฅผ ๋ด๊ณ ์๋ ํจ์์ ๊ธฐ๋ฅ์ ์ฌ์ฉํ ์ ์๋ค.
2. ํฌ์ธํฐ funcptr ์ด ํจ์ func์ ์ฃผ์๋ฅผ ๊ฐ์ง๊ณ ์๊ธฐ ๋๋ฌธ์ func(3)์ funcptr(3) ์ด๋ ๊ฒ๋ ํธ์ถํ ์ ์๋ค.
๐funcptr = func2
1. ์ด๋ ๊ฒ ํจ์ ํฌ์ธํฐ์ ๋ค๋ฅธ ํจ์๋ฅผ ๋์
์ ํ๋ ค๋ฉด ๋ฆฌํดํ์
, ๋งค๊ฐ๋ณ์ํ์
, ๋งค๊ฐ๋ณ์์ ๊ฐ์๊ฐ ์๋ก ์ผ์นํด์ผ ๊ฐ๋ฅ ํ๋ค.
- funcptr์ int (*funcptr)(int) ํ์
์ด๋ฏ๋ก func2 ํจ์ ๋ํ ๋ฆฌํดํ์
์ด int๊ณ ๋งค๊ฐ๋ณ์ ํ์
๋ int๊ณ ๋งค๊ฐ๋ณ์๋ 1๊ฐ ์ฌ์ผ funcptr์ ์ฃผ์ ๋์
์ด ๊ฐ๋ฅํ๋ค
๐ ๋งค๊ฐ๋ณ์๋ก ์ฌ์ฉํ ์ ์๋ ํจ์ ํฌ์ธํฐ
ํจ์ ํฌ์ธํฐ๋ ๋ค๋ฅธ ๋ณ์์ ๋์ผํ๊ฒ ๋งค๊ฐ๋ณ์๋ก ์ฌ์ฉ์ด ๊ฐ๋ฅํฉ๋๋ค.
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
|
#include <iostream>
using namespace std;
int add(int a, int b)
{
return (a + b);
}
void print_odd(int a, int b, int (*f)(int, int)) //๋งค๊ฐ๋ณ์๋ก ํจ์ add๋ฅผ ๋ฐ์
{
int ret = f(a, b);
if (ret % 2 == 1)
cout << ret << endl;
else
cout << "ํ์๊ฐ ์๋์์!" << endl;
}
int main(void)
{
print_odd(2, 4, add);
print_odd(2, 3, add);
}
//์ถ๋ ฅ
//ํ์๊ฐ ์๋์์!
//5
|
cs |
๐ ํจ์ํฌ์ธํฐ์ const
ํจ์ํฌ์ธํฐ๋ ๋ค๋ฅธ ๋ณ์๋ค๊ณผ ๋์ผํ๊ฒ const๋ฅผ ์ฌ์ฉํ ์ ์์ต๋๋ค.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
#include <iostream>
using namespace std;
int add(int a, int b)
{
return (a + b);
}
int main(void)
{
int (*const f)(int, int) = add;
//์ฐธ์กฐ ์ฐ์ฐ์ *์์ const๋ฅผ ๋ถ์ฌ์ฃผ๋ฉด ํจ์ํฌ์ธํฐ f๋ฅผ ์์ํ ์ํฌ ์ ์์ต๋๋ค.
cout << f(1, 2);
}
//์ถ๋ ฅ
//3
|
cs |
๐ class ๋ฉค๋ฒํจ์์ ํจ์ํฌ์ธํฐ ์ฌ์ฉ
class์ ๋ฉค๋ฒํจ์๋ ํจ์ํฌ์ธํฐ๋ก ์ฌ์ฉ์ด ๊ฐ๋ฅํฉ๋๋ค.
ํ์ง๋ง class๋ด๋ถ์ ๋ฉค๋ฒํจ์๋ ๊ฐ์ฒด๋ฅผ ํตํด์๋ง ์ถ๋ ฅ์ด ๊ฐ๋ฅํ๊ณ , ํจ์ํฌ์ธํฐ์ ์ ์ธ ๋ํ ํด๋น ํด๋์ค์ ๋ฒ์ ๋ด์์๋ ํจ์ํฌ์ธํฐ๋ผ๊ณ ๋ช
์ํด์ค์ผ ์ฌ์ฉ์ด ๊ฐ๋ฅํ๋ค.
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
|
#include <iostream>
using namespace std;
class Test
{
public:
int add(int a, int b) //ํด๋์ค Test์์ ๋ฉค๋ฒํจ์ add๊ฐ ์์ต๋๋ค.
{
return (a + b);
}
};
int main(void)
{
Test test; // ํด๋์ค Test๋ฅผ ์ธ์คํด์คํ ์์ผ ๊ฐ์ฒด๋ก ๋ง๋ญ๋๋ค.
int (Test::*f)(int, int) = &Test::add;
//ํจ์ํฌ์ธํฐ f๋ Test์ ๋ฉค๋ฒํจ์๋ฅผ ์ฌ์ฉํ๊ฒ ๋ค๊ณ Test::๋ฅผ ํตํด ๋ช
์ํด์ค๋๋ค.
//๊ทธ๋ฆฌ๊ณ ํจ์์ ์ฃผ์ ๋ํ Test์ ๋ฉค๋ฒํจ์๋ผ๊ณ ๋ช
์ํด ์ฃผ๊ณ ๋ค๋ฅธ ํจ์ ํฌ์ธํฐ์ ๋ฌ๋ฆฌ
//ํญ์ ๋ช
์์ ์ผ๋ก &์ฐ์ฐ์๋ฅผ ํตํด ํจ์์ ์ฃผ์๋ฅผ ๊ฐ์ ธ์์ผ ํฉ๋๋ค.
cout << (test.*f)(1, 2) << endl;
//ํจ์ํฌ์ธํฐ์ ์ฌ์ฉ ๋ํ ํด๋น ํด๋์ค์ ๊ฐ์ฒด๋ฅผ ํตํด์ ์ฌ์ฉํด์ผ ํฉ๋๋ค.
//๋ง์ฝ class์ ๋ฉค๋ฒํจ์ ๋ด์์ ํจ์ํฌ์ธํฐ๋ฅผ ์ฌ์ฉํ๋ค๋ฉด this๋ฅผ ํตํด์ ์ ๊ทผํ ์ ์์ต๋๋ค.
//์ ๊ทผ์ ๋ฉค๋ฒ ํฌ์ธํฐ ์ฐ์ฐ์์ธ .* ๋๋ ->*์ฐ์ฐ์๋ก ์ ๊ทผํด์ผ ํฉ๋๋ค.
}
//์ถ๋ ฅ
//3
|
cs |
๐ static์ ์ด์ฉํ ๋ฉค๋ฒํจ์์ ์ ๊ทผ
์ผ๋ฐ ํจ์ํฌ์ธํฐ์ ๋น์ทํ๊ฒ ์ฌ์ฉํฉ๋๋ค.
class์์ ๋ฉค๋ฒํจ์์ static์ ์ฌ์ฉํ๋ฉด class์ ์ด๋ฆ์ผ๋ก ํด๋น ํจ์์ ์ ๊ทผ์ด ๊ฐ๋ฅํ๊ณ , ์ด๋ค ํน์ ๊ฐ์ฒด์ ๊ท์๋๋๊ฒ ์๋๋ผ๊ณ ํ์์ต๋๋ค.
๊ทธ๋ ๊ธฐ ๋๋ฌธ์ class์ ์ด๋ฆ์ ํตํด์ ์ ๊ทผ๋ง ํด์ฃผ๋ฉด ์ผ๋ฐ ํจ์ํฌ์ธํฐ์ ๋์ผํ๊ฒ ์ฌ์ฉ์ด ๊ฐ๋ฅํฉ๋๋ค.
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
|
#include <iostream>
using namespace std;
class Test //class๋ฅผ ์ด 4๊ฐ ์์ฑํ ๋ค ๋ฉค๋ฒํจ์ ๋ชจ๋ static์ ์ฌ์ฉํด ์ค๋๋ค.
{
public:
static int add(int a, int b)
{
return (a + b);
}
};
class TestTwo
{
public:
static int sub(int a, int b)
{
return (a - b);
}
};
class TestThree
{
public:
static int div(int a, int b)
{
return (a / b);
}
};
class TestFour
{
public:
static int mul(int a, int b)
{
return (a * b);
}
};
int main(void)
{
//ํจ์ํฌ์ธํฐ์ ๋ฐฐ์ด์ ์ ์ธํด์ฃผ๊ณ , class์ ์ด๋ฆ์ ํตํด์ ํจ์์ ์ฃผ์๋ฅผ ์ ์ฅํด์ค๋๋ค.
int (*f[4])(int, int) = {
&Test::add,
&TestTwo::sub,
&TestThree::div,
&TestFour::mul
};
for (int i = 0; i < 4; i++)
cout << f[i](1, 2) << endl;
//์ถ๋ ฅ์ ์ผ๋ฐ ํจ์ํฌ์ธํฐ์ ๋์ผํฉ๋๋ค.
}
//์ถ๋ ฅ
//3
//-1
//0
//2
|
cs |
'C++' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
์ผ๊ฐ๋น, ์ผ๊ฐํจ์ (0) | 2022.12.07 |
---|---|
์๋์ฐ ํ๋ก์์ , ์๋์ฐ ๋ฉ์ธ์ง, ๋ฉ์์ง ํ, ๋ฉ์์ง ๋ฃจํ (0) | 2022.12.05 |
๋ณต์ฌ ์์ฑ์(Copy Constructor), ํฉํ ๋ฆฌ ํจํด (Factory Pattern) (0) | 2022.11.29 |
๊ฐ์ํจ์(virtual function), ํจ์ ์ฌ์ ์ (function redefine) (0) | 2022.11.25 |
์์ฑ์(Constructor), ์๋ฉธ์(Destructor) (0) | 2022.11.24 |