티스토리 뷰

LANGUAGE/C++

[C++] OPERATOR FUNCTION - 연산자 함수

진심스테이크 2018. 3. 14. 15:36

 

연산자 함수 : 객체를 가지고 연산 처리를 할 수 있게 해주는것

- 값 + 객체

- 원래 의미를 바꾸지 말라 -> 이유 : 동적 메모리 할당

- 새로운 연산자를 만들면 안된다

- friend를 많이 쓰는 대표적인 함수

 

 

기본 형태

리턴타입 operator 연산자(매개변수 리스트);

 

 

#default로 존재하는 함수

- 생성자 소멸자 복사 대입연산자 中 대입연산자 사용 

 

#복사 생성자 : 객체 생성 할 때 객체를 대입하기 위해 만듬

#대입 : 객체 생성 이후에 객체를 대입하는것

 

방법

1. 멤버 함수

- this가 있다 : 자기 자신으로 넘길 수 있다

#include "stdafx.h"
#include <iostream>
using namespace std;

class A {
    int a;
public :
    A(int a = 1000) { this->a = a; }
    void setA(int a) { this->a = a; }
    int getA() { return a; }
    int operator+(int data) {
        return this->a + data;
    }
};

void main() {
    cout << 3 + 4 << endl;
    A aa(10);

    //aa.operator+(4) - 멤버 함수
    cout << aa + 4 << endl;
}
 

 

2. 외부 함수

- friend로 지정해서 멤버 함수와 같은 서비스를 제공

- static 함수

#include "stdafx.h"
#include <iostream>
using namespace std;

class A {
    int a;
public :
    A(int a = 1000) { this->a = a; }
    void setA(int a) { this->a = a; }
    int getA() { return a; }
    friend int operator+(const A &aa, int data);
};

int operator+(const A &aa, int data) { //외부 함수
    //aa는 직접 접근  -레퍼런스
    return aa.a + data;
}

void main() {
    A aa(10);

    //operator+(aa, 4) - 외부 함수
    cout << aa + 4 << endl;
}

 


 

default 대입 연산자 함수

1. 얕은 대입 : 값을 복사

#include "stdafx.h"
#include <iostream>
using namespace std;

class A {
    int a;
public :
    A(int a = 1000) { this->a = a; }
    void setA(int a) { this->a = a; }
    int getA() { return a; }
    int operator+(int data) { return this->a + data; }
    A& operator=(const A &aa) { //레퍼런스로 자기 자신을 보내겠다
        a = aa.a; //this가 bb를 가리키고 있다
        return *this; //자기 자신을 보내겠다 ->복사된 b가 리턴
    }
};

void main() {
    A aa(10);
    A bb;
    A cc;

    cout << aa.getA() << endl;
    cout << bb.getA() << endl; //디폴트 함수

    //bb.operator=(aa); //bb = aa;
    cc = bb = aa; //cc.operator=(bb.operator=(aa)); -연속 카피
    //bb = aa;
    //cc = bb;
    cout << aa.getA() << endl;
    cout << bb.getA() << endl;
    cout << cc.getA() << endl;
}

 

 

2. 깊은 대입 - 포인터가 가리키는 값을 복사

#include "stdafx.h"
#include <iostream>
using namespace std;

class A {
    int *p;
public :
    A() {
        p = new int;
        *p = 0;
    }
    ~A() { delete p; }
    void setA(int data) { *p = data; }
    int getA() const { return *p; }
    A(const A &aa) {
        p = new int;
        *p = *aa.p;
    }

    A& operator=(const A &aa) {
        //최적화와 안전화를 위해 사용
        if (this == &aa) { //aa = aa; 일때 예외처리
            return *this;
        }
        delete p; //bb.p가 가리키는 값을 삭제 - 다시 정의해서 복사 하겠다

        p = new int;
        *p = *aa.p; //aa가 가르키는 값
        return *this;
    }
};

void main() {
    A aa;
    A bb;

    aa.setA(90);
    cout << aa.getA() << endl; //90
    cout << bb.getA() << endl; //0
    
    bb = aa;
    //메모리가 있는데 버림
    cout << aa.getA() << endl; 
    cout << bb.getA() << endl;
}

 

선언을 기준으로 *의 갯수가 같으면 값, 모자르면 주소

 

 


 

 

#JAVA에서의 toString의 역할을 C++에서 사용할 때 

cout << aa; //멤버 함수 못부름

cout.operator << (aa); //불가능

-> operator << (cout, aa);

#include "stdafx.h"
#include <iostream>
using namespace std;

class A {
    int a;
public :
    A(int a = 1000) { this->a = a; }
    void setA(int a) { this->a = a; }
    int getA() { return a; }
    friend ostream& operator<<(ostream &cout, const A &aa); //직접 접근 불가해서 friend로 지정
    friend istream& operator>>(istream &cin, A &aa); //값을 읽기만 할 때 const로 지정 
};

ostream& operator<<(ostream &cout, const A &aa) {
    cout << aa.a;
    return cout;
}

istream& operator>>(istream &cout, A &bb) {
    cin >> bb.a;
    return cin;
}

void main() {
    A aa;
    A bb(90);

    cin >> bb; //operator>>(cin, bb)
    cin >> aa >> bb;
    cout << aa << "\t" << bb << endl;

    cout << aa.getA() << endl;
    cout << aa << bb; //operator<<(cout, aa);

 

#cout은 ostream에 있고, cin은 istream에 있다

 

//주석 처리는 외부

#include "stdafx.h"
#include <iostream>
using namespace std;

class A {
    int a;
public:
    A(int a = 1000) { this->a = a; }
    void setA(int a) { this->a = a; }
    const int getA() { return a; }
    friend ostream& operator<<(ostream &cout, const A &aa); //직접 접근 불가해서 friend로 지정
    friend istream& operator>>(istream &cin, A &aa); //값을 읽기만 할 때 const로 지정
    //friend int operator+(const A &aa, const A &bb);
    A& operator=(const A &aa) {
        a = aa.a;
        return *this;
    }

    A operator+(const A &aa){
        A tmp; //tmp는 함수가 끝나면 사라짐
        tmp.a = a + aa.a;
        return  tmp;
    }
};

/*
int operator+(const A &aa, const A &bb) { 
    return aa.a + bb.a;
}
*/

ostream& operator<<(ostream &cout, const A &aa) {
    cout << aa.a;
    return cout;
}

istream& operator>>(istream &cout, A &bb) {
    cin >> bb.a;
    return cin;
}

void main() {
    A aa;
    A bb;
    A cc;

    cin >> aa >> bb;
    cc = aa + bb; //입력을 하고 대입
    //내부 - cc.operator=(aa.operator+(bb))
    //외부 - operator=(cc, operator+(aa, bb))

    cout << aa << endl;
    cout << bb << endl;
    cout << cc << endl;
}

 

'LANGUAGE > C++' 카테고리의 다른 글

[C++] TEMPLATE - 템플릿  (0) 2018.03.16
[C++] STRING 직접 구현  (0) 2018.03.15
[C++] FRIEND  (0) 2018.03.14
[C++] STACK / QUEUE  (0) 2018.03.14
[C++] VIRTUAL FUNCTION - 가상 함수  (0) 2018.03.13
댓글