Minwoo Dev.

[C++] 전위 증감 연산자 오버로딩, ++pos, --pos 본문

C++

[C++] 전위 증감 연산자 오버로딩, ++pos, --pos

itisminu 2024. 7. 19. 10:56
728x90
반응형
SMALL

사칙연산 연산자를 오버로딩 해보았으니, 이번에는 증감 연산자 오버로딩을 해보겠다.

 

증감 연산자

증감 연산자는 증가 연산자, 감소 연산자로 나뉜다.

 

증가 연산자 (전위 연산자)

++num;

 

num에 1이라는 값을 먼저 증가시킨 뒤 해당 라인의 연산을 수행한다.

 

감소 연산자 (전위 연산자)

--num;

 

 

 

num에 1이라는 값을 먼저 감소시킨 뒤 해당 라인의 연산을 수행한다.

 

즉, 전위 연산자는 해당 라인의 다른 연산자보다 전위 연산자가 우선순위를 갖는 것이다.

 

 


증가 연산자 구현(operator++)

#include<iostream>
using namespace std;

class Point{
private:
    int xpos, ypos;
public:
    Point(int x = 0, int y = 0):xpos(x),ypos(y){}
    void ShowPosition() const{
        cout << '[' << xpos << ", " << ypos << ']' << endl;
    }
};


int main(void){
    Point pos1(3, 4);
    Point pos2 = ++pos1;// 증가 연산자 사용됨
    pos2.ShowPosition();
    return 0;
}

 

위 코드에서는 Point 클래스의 객체에 증가 연산자를 사용하고자 하고있다.

 

 

증감 연산자 중에서도 전위 증감 연산자의 연산 순서를 생각해보면, 1을 먼저 더하고 -> 자기자신을 반환하면 된다.

이대로 구현해보겠다.

 

멤버 함수로 구현

#include<iostream>
using namespace std;

class Point{
private:
    int xpos, ypos;
public:
    Point(int x = 0, int y = 0):xpos(x),ypos(y){}
    void ShowPosition() const{
        cout << '[' << xpos << ", " << ypos << ']' << endl;
    }
    Point operator++(){ // ++ 연산자 오버로딩!
    	xpos+=1;
        ypos+=1;
        return *this;
    }
};


int main(void){
    Point pos1(3, 4);
    Point pos2 = ++pos1;
    pos2.ShowPosition();
    return 0;
}

 

실행 결과

 

 

증가 연산자는 특정 값을 더하는 것이 아니라, 1을 그냥 더하는 것이므로 매개변수로 아무 값도 필요하지 않게 된다.

 

여기서 *this는 자기 자신을 의미한다

this는 자기 자신의 주소값, *this는 역참조를 하여 자기자신을 의미하는 값 자체가 되는 것이다.

 

 

전역 함수로 구현

#include<iostream>
using namespace std;

class Point{
private:
    int xpos, ypos;
public:
    Point(int x = 0, int y = 0):xpos(x),ypos(y){}
    void ShowPosition() const{
        cout << '[' << xpos << ", " << ypos << ']' << endl;
    }
    friend Point operator++(const Point &pos);
};

Point operator++(const Point &pos){
    	pos.xpos+=1;
        pos.ypos+=1;
        return pos;
}

int main(void){
    Point pos1(3, 4);
    Point pos2 = ++pos1;
    pos2.ShowPosition();
    return 0;
}

 

실행 결과

 

전역 함수로의 구현에서도, 다른 값이 필요하지 않으므로 Point &pos 하나만 매개변수로 받아오게 된다.

 

 


감소 연산자 구현(operator--)

#include<iostream>
using namespace std;

class Point{
private:
    int xpos, ypos;
public:
    Point(int x = 0, int y = 0):xpos(x),ypos(y){}
    void ShowPosition() const{
        cout << '[' << xpos << ", " << ypos << ']' << endl;
    }
};


int main(void){
    Point pos1(3, 4);
    Point pos2 = --pos1; // -- 연산자 오버로딩 필요!!
    pos2.ShowPosition();
    return 0;
}

 

이번에는 감소 연산자를 구현해보겠다.

 

이 경우에도 값을 1 감소 -> 자기자신을 반환 순서로 수행된다.

 

멤버 함수로 작성

#include<iostream>
using namespace std;

class Point{
private:
    int xpos, ypos;
public:
    Point(int x = 0, int y = 0):xpos(x),ypos(y){}
    void ShowPosition() const{
        cout << '[' << xpos << ", " << ypos << ']' << endl;
    }
    Point operator--(){
    	xpos-=1;
        ypos-=1;
        return *this;
    }
};


int main(void){
    Point pos1(3, 4);
    Point pos2 = --pos1;
    pos2.ShowPosition();
    return 0;
}

 

실행 결과

 

 

전역 함수로 작성

#include<iostream>
using namespace std;

class Point{
private:
    int xpos, ypos;
public:
    Point(int x = 0, int y = 0):xpos(x),ypos(y){}
    void ShowPosition() const{
        cout << '[' << xpos << ", " << ypos << ']' << endl;
    }
    friend Point operator--(const Point &p);
};

Point operator--(const Point &p){
	p.xpos-=1;
    p.ypos-=1;
    return p;
}

int main(void){
    Point pos1(3, 4);
    Point pos2 = --pos1;
    pos2.ShowPosition();
    return 0;
}

 

실행 결과

 

 

작성 방식은 위와 똑같다.

 

 

 

 


 

 

 

 

 

 

 

 

후위 연산자는 이것보다 조금 더 복잡하기 때문에 다음 게시글에서 설명하겠다.
728x90
반응형
LIST