dev-ima
이마의 개발 블로그
dev-ima
전체 방문자
오늘
어제
  • 분류 전체보기 (16)
    • deeplearning study (4)
    • toy project (3)
    • algorithm study (0)
    • Portfolio (0)
    • Design Pattern (1)
    • Computer Vision (3)
    • c++ (4)
    • MLops (1)

블로그 메뉴

  • 홈
  • 태그
  • 방명록

공지사항

인기 글

태그

  • machine learning
  • ML시스템
  • MLops
  • Computer Vision
  • ML system
  • DEEPLEARNING
  • UML 다이어그램
  • ML workflow
  • image Enhancement
  • kaggle
  • Unsupervised
  • static member
  • instance segmetation
  • cv
  • Python
  • C++
  • Object detection
  • Face Recognition
  • 정적멤버변수
  • UW-Madison GI Tract Image Segmentation
  • Recognition
  • 머신러닝
  • 정적멤버함수
  • 비지도
  • static member variable
  • DETR
  • static member method
  • TITANIC
  • Data의 종류
  • 딥러닝 시스템

최근 댓글

최근 글

티스토리

hELLO · Designed By 정상우.
dev-ima

이마의 개발 블로그

c++

함수 포인터

2023. 2. 15. 21:15

정의

함수 포인터: 함수의 주소를 담고있는 포인터 변수

함수 포인터의 기본형태 : int (*ptrFunc) (int a, int b)

  • 반환 데이터 타입: int형, 함수 포인터명: ptrFunc, 매개변수: int a, int b
  • ptrFunc이라는 함수 포인터는 반환데이터 타입을 int형으로 가지며 int형 변수 a와 b를 매개변수로 가진다.

쓰임새

함수 포인터는 다른 함수의 매개변수로 함수를 받고 싶을 때 사용된다. 

보통 callback 함수로 사용된다.

callback 함수

  • a함수의 인자로 b함수의 함수 포인터를 넘겨 a함수가 실행 될때, b함수도 실행하게하는 것이다.
  • 주로 라이브러리의 유연성을 제공하기위해 구현된다. 사람마다 원하는 세부기능이 다를 수 있기 때문에 사용자의 함수를 직접작성하여 라이브러리에 구현된 함수의 함수포인터로 넘겨줄 수 있다.

예시 코드

int add(int x, int y, int(*fp_x)(int), int(*fp_y)(int)){
	
    if(fp_x != NULL){
    	x = fp_x(x)
    }

    else if(fp_y != NULL){
    	y = fp_x(y)
    }
    return x + y;
}

int positive(int a){
	if(a<0) return -a;
    return a;
}

int square(int a){
	return a*a;
}

int main(){
	
    add(1, 3, NULL,NULL);
    add(3, 6, positive, NULL);
    add(7, 5, NULL, positive);
    add(4, 4, square, positive);

}

'c++' 카테고리의 다른 글

정적 멤버 함수(static member variable)  (0) 2023.04.05
메모리의 stack영역, heap영역  (0) 2023.02.16
함수 오버로딩  (0) 2023.02.11
    'c++' 카테고리의 다른 글
    • 정적 멤버 함수(static member variable)
    • 메모리의 stack영역, heap영역
    • 함수 오버로딩
    dev-ima
    dev-ima

    티스토리툴바