본문 바로가기
카테고리 없음

C++ operator function & assignment operator

by 리승연 2018. 9. 21.
반응형

(필드를 포인터로 생성하면 생성자=동적, 소멸자-반환, 복사생성자-깊은복사, 대입연산자-깊은복사로 해야한다)

2018/09/21 - [Progamming/C++ 실습] - [C++ 실습] operator function quiz_9190

 

 

'='은 대입연산자, 연산자 함수에 포함
연산자함수안에 '+', '-', '/' 등의 많은 함수가 있다.

 

 

 

operator function 사용 방법 2가지

 

1. 멤버함수 (형태: aa.operator+(bb))

 

2. 외부함수 (형태: operator+(aa,bb))

 

 

 

operator function 사용 시

 

●연산자함수를 만들 때는 원래 의미를 바꾸면 안된다. 오류는 안 나지만 암묵적 약속이다.

 

새로운 연산자 함수를 만들면 안 된다. (++인데 +++불가능)

 

 

 

operator function 왜 사용 하는가?

 

연산자,객체 또는 객체,객체(객체+객체) 연산처리를 도와주는 기능(편의성)을 제공

 

 

 

 

 

 

 


 

 

 

연산자 함수:멤버함수로 사용할 경우

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
using namespace std;
 
class A {
    int a;
public:
    A(int num):a(num){}
    void setA(int a) { this->= a; }
    int getA() { return a; }
 
    int operator+(const A &bb)
    {return this->a+bb.a;}
};


 
 
void main(){
    A aa(3);
    A bb(4);
    //cout << aa.getA() + bb.getA() << endl;
    cout << aa + bb << endl//멤버일 경우  aa.operator+(bb)
}
cs

 

 

연산자 함수:외부함수로 사용할 경우

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 A {
    int a;
public:
    A(int num) :a(num) {}
    void setA(int a) { this->= a; }
    int getA() { return a; }
 
 
    friend int operator+(const A &aa, const A &bb);
 
};
 
int operator+(const A &aa, const A &bb){
    //return aa.getA() + bb.getA();
    return aa.a + bb.a;
}
 
void main() {
    A aa(3);
    A bb(4);
 
    cout << aa + bb << endl//멤버일 경우  aa.operator+(bb)
                            //operator+(aa,bb)
    cout << aa + 10 << endl;   
}
cs

 

연산자:외부함수로만 가능 한 경우 

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
#include <iostream>
using namespace std;
 
class A {
    int a;
public:
    A(int num) :a(num) {}
    void setA(int a) { this->= a; }
    int getA() { return a; }
 
    int operator+(const A &bb) {
        return this->+ bb.a;
    }
    friend int operator+(int aa, const A &bb);
};
 
int operator+(int aa, const A &bb) {
    return 10 + bb.a;
}
 
 
void main() {
    A aa(3);
    
    cout << aa + 10 << endl//멤버&외부가능
    cout << 10 + aa << endl//멤버함수 생성불가능 맴버함수 만들려면 객체.()으로 접근해야함
                            //10은 객체가 아님.
 
}
cs

 


 

assginment operator

 

 

 

 

복사생성자 조건 이외에 다 대입연산자

 

 

 

대입연산자:shallow copy(얕은복사)

 

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
#include <iostream>
using namespace std;
 
class A {
    int a;
public:
    A(int a = 0) { this->= a; }
    void setA(int a) { this->= a; }
    int getA() { return a; }
        
    //얕은대입연산자함수는 디폴트로 제공
    A& operator=(const A &aa) {
        if (this == &aa) return *this; //aa=aa
        this->a = aa.a;
        return *this;
    } //<-형태로 default 제공
};
 
void main() {
    A aa(10);
    A bb;
    A cc;
 
    cout << aa.getA() << endl;
    cout << bb.getA() << endl;
 
    cc = bb = aa; //대입연산자 호출 //cc.operator=(bb.operator=(aa))
                //bb.operator=(aa) 디폴트로존재
 
    cout << aa.getA() << endl;
    cout << bb.getA() << endl;
 
};
 
cs

 

 

 

대입연산자:deep copy(깊은복사)

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
#include <iostream>
using namespace std;
 
class A {
    int *a;
public:
    A(int a = 0) { this->= new int*(this->a) = a; }
    ~A() { delete a; }
    void setA(int a) { *(this->a) = a; }
    int getA() { return *a; }
    
    //깊은대입연산자 함수 
    //필드를 포인터 할때 생성자-동적, 소멸자-반환, 복사생성자-깊은복사, 대입연산자=깊은복사
      A& operator=(const A &aa) {
        if (this == &aa) return *this//aa=aa 예외처리        
        *this->= *aa.a;
        return *this;
    }
};
    void main() {
        A aa(10);
        A bb;
        A cc;
 
        cout << aa.getA() << endl;
        cout << bb.getA() << endl;
 
        cc = bb = aa; //대입연산자 호출 //cc.operator=(bb.operator=(aa))
                    //bb.operator=(aa) 디폴트로존재
 
        cout << aa.getA() << endl;
        cout << bb.getA() << endl;
        cout << cc.getA() << endl;
    }
 
 
 

 

반응형