갓똥
나는야 프로그래머
갓똥
전체 방문자
오늘
어제
  • 분류 전체보기 (186)
    • 프로그래밍 (146)
      • 자바 (9)
      • 안드로이드 (2)
      • 유니티 (20)
      • C++ (38)
      • C# (56)
      • HTML (2)
      • 파이썬 (3)
      • 자료구조 (2)
      • 알고리즘 (0)
      • 문제풀이 (4)
      • 디자인 패턴 (7)
      • 카카오톡 봇 (1)
      • 엑셀 (1)
      • 기타 (1)
    • 게임 (21)
      • 테일즈위버 (0)
      • 카이로소프트 (1)
      • 순위 (19)
      • 기타 (1)
    • 일상 (13)
      • 카페 (1)
      • 방탈출 (12)
    • 기타 (6)
      • 웃긴자료 (5)

블로그 메뉴

  • 홈
  • 방명록

공지사항

인기 글

태그

  • c# 코루틴
  • 게임 매출 순위
  • 유니티 그래프
  • 전세계게임매출순위
  • 게임 디자인 패턴
  • 강남 방탈출
  • pc 게임 순위
  • 자바
  • C++ 소멸자
  • 2020년 게임 매출
  • c# delegate
  • C++ virtual
  • 유니티 골드그래프
  • c# coroutine
  • C# boxing
  • 알고리즘
  • 글로벌게임매출
  • c# unboxing
  • C# 예외 처리
  • 유니티 그래프 그리기
  • C++ 상속
  • pc게임 순위
  • 롤 골드그래프
  • Unity Graph
  • c# collection
  • c# Thread
  • 전세계 게임 매출
  • C++
  • 모바일 게임 순위
  • 게임매출순위

최근 댓글

최근 글

티스토리

hELLO · Designed By 정상우.
갓똥

나는야 프로그래머

[C++] 연산자 재정의 (operator overloading)
프로그래밍/C++

[C++] 연산자 재정의 (operator overloading)

2019. 12. 14. 14:47
728x90
반응형

1. 연산자 재정의 기본 개념

#include <iostream>

class Point {
    int x;
    int y;
public:
    Point( int a = 0, int b = 0 ) : x(a), y(b) {}
};

int main() {
    int n = 1 + 2; // 3
    
    Point p1(1, 1);
    Point p2(2, 2);
    Point p3 = p1 + p2; // ?
    // operator+(p1, p2) -> operator+(Point, Point)
}

  ① +, -, *, 등의 연산자도 함수로 만들 수 있다.

    => operator+, operator-, operator*

 


2. a + b 를 컴파일러가 해석하는 방법

  ① a, b가 모두 primitive type (int, double 등) 인 경우

    => 일반적인 덧셈을 수행한다.

 

  ② a, b중 한 개라도 사용자 정의 타입이 있는 경우

    => operator+ 함수를 찾게 된다.

    => 방법1. 멤버 함수 검색

        ▶ a.operator+(b)

    => 방법2. 멤버가 아닌 일반 함수 검색

        ▶ operator+(a, b)

 


3. 멤버가 아닌 일반 함수로 구현하는 operator+

#include <iostream>

class Point {
    int x;
    int y;
public:
    Point( int a = 0, int b = 0 ) : x(a), y(b) {}
    
    void print() const {
        std::cout << x << ", " << y << std::endl;
    }
    
    friend Point operator+(const Point& p1, const Point& p2);
};

Point operator+(const Point& p1, const Point& p2) {
    Point temp;
    temp.x = p1.x + p2.x;
    temp.y = p1.y + p2.y;
    return temp;
}

int main() {
    Point p1(1, 1);
    Point p2(2, 2);
    Point p3 = p1 + p2; // operator+(p1, p2)
    
    p3.print();
}

  ① +는 이항 연산자 이므로 인자가 2개 이어야 한다.

 

  ② 타입의 크기가 큰 경우 call by value보다는 const 참조가 좋다.

 

  ③ friend 함수로 만드는 경우가 많다.

 


4. 멤버 함수로 구현하는 operator+

#include <iostream>

class Point {
    int x;
    int y;
public:
    Point( int a = 0, int b = 0 ) : x(a), y(b) {}
    
    void print() const {
        std::cout << x << ", " << y << std::endl;
    }
    
    // 멤버로 만드는 operator+
    Point operator+( const Point& p ) {
        Point temp;
        temp.x = p.x + x;
        temp.y = p.y + y;
        return temp;
    }
};

int main() {
    Point p1(1, 1);
    Point p2(2, 2);
    Point p3 = p1 + p2; // p1.operator+(p2)
    
    p3.print();
}

  ① +는 이항 연산자 이지만 인자가 1개 이어야 한다.

 


5. 멤버 함수 vs 멤버가 아닌 함수

#include <iostream>

class Point {
    int x;
    int y;
public:
    Point( int a = 0, int b = 0 ) : x(a), y(b) {}
    
    void print() const {
        std::cout << x << ", " << y << std::endl;
    }
    
    // 멤버로 만드는 operator+
    Point operator+( const Point& p ) {
        std::cout << "member" << std::endl;
    
        Point temp;
        temp.x = p.x + x;
        temp.y = p.y + y;
        return temp;
    }
    
    friend Point operator+(const Point& p1, const Point& p2);
};

// 일반 함수로 구현
Point operator+(const Point& p1, const Point& p2) {
    std::cout << "non member" << std::endl;
    
    Point temp;
    temp.x = p1.x + p2.x;
    temp.y = p1.y + p2.y;
    return temp;
}

int main() {
    Point p1(1, 1);
    Point p2(2, 2);
    Point p3 = p1 + p2; // 1. p1.operator+(p2)
                        // 2. operator+(p1, p2)
    p3.print();
}
실행결과

member
3, 3

  ① operator+ 함수의 인자의 개수

    => 멤버 함수 : 1개

    => 멤버가 아닌 함수 : 2개

 

  ② 멤버와 멤버가 아닌 함수를 동시에 제공하면

    => 멤버함수가 우선 시 된다.

 

int main() {
    Point p1(1, 1);
    Point p2(2, 2);
    Point p3 = p1 + p2; // 1. p1.operator+(p2)
                        // 2. operator+(p1, p2)
                        
    p1 + 1; // 1. p1.operator+(int)
            // 2. operator+(Point, int)
    
    1 + p1; // 1. 1.operator+(Point) - 만들 수 없다.
            // 2. operator+(int, Point) - 만들 수 있다.
}

  ③ a + b에서 첫 번째 인자인 a의 타입이

    => user type이면

        ▶ 멤버 함수와 멤버가 아닌 함수 모두 사용가능

 

    => user type이 아니면

        ▶ 멤버가 아닌 일반 함수로만 만들 수 있다.

 

  ④ 멤버 함수가 좋을까 ? 멤버가 아닌 함수가 좋을까 ? 

    => 개발자들마다 의견이 다름...

 


6. operator+ 함수를 호출하는 방법

int main() {
    Point p1(1, 1);
    Point p2(2, 2);
    
    Point p3 = p1 + p2; // 1. 컴파일러에 의해
    operator+(p1, p2);  // 2. 일반 함수로
    p1.operator+(p2);   // 3. 멤버 함수로
}

  ① 컴파일러에 의해

    => Point p3 = p1 + p2;

    => 멤버 함수 우선

 

  ② 일반 함수로

    => operator+(p1, p2);

 

  ③ 멤버 함수로

    => p1.operator+(p2);

728x90
반응형

'프로그래밍 > C++' 카테고리의 다른 글

[C++] 연산자 재정의 주의 사항  (0) 2019.12.14
[C++] 다중 상속 (multiple inheritance)  (0) 2019.12.03
[C++] RTTI (Run Time Type Information)  (0) 2019.12.02
[C++] 인터페이스 (interface)  (0) 2019.12.01
[C++] 추상 클래스 (abstract class)  (0) 2019.11.30
    '프로그래밍/C++' 카테고리의 다른 글
    • [C++] 연산자 재정의 주의 사항
    • [C++] 다중 상속 (multiple inheritance)
    • [C++] RTTI (Run Time Type Information)
    • [C++] 인터페이스 (interface)
    갓똥
    갓똥
    공부하며 알아가는 내용을 정리해 봅니다.

    티스토리툴바