👨🏻‍💻 programming/◽ c, c++

[C++] 연산자 오버로딩 프로토타입 & 한계

핑크코냥 2022. 2. 8. 00:59
728x90
종류  연산자 프로토타입 
이항 산술 연산자 operator+
operator-
operator*
operator/
operator%
T operator■ ( const T&, const T& ) ;
단항 산술 및 비트 연산자 operator+
operator-
operator~
T operator■ ( ) const ;
선행, 후행 (증가,감소)  operator++
operator--
T& operator■ ( );
T& operator■ ( int );
대입 연산자 operator =  T& operator■ ( const T& );
축약 산술 대입 연산자 operator+=
operator-=
operator*=
operator/=
operator%=
T& operator■ ( const T& );
T& operator■ ( const E& );
이항 비트 연산자 operator<<
operator>>
operator&
operator|
operator^
T operator■ ( const T&, const T& );
T operator■ ( const T&, const E& );
이항 비트 대입 연산자 operator<<=
operator>>=
operator&=
operator|=
operator^=
T& operator■ ( const T& );
T& operator■ ( const E& );
이항 비교 연산자 operator<
operator>
operator<=
operator>=
operator==
operator!=
bool operator■ ( const T&, const T& );
bool operator■ ( const T&, const E& );
I/O 스트림 연산자 operator<<
operator>>
ostream& operator<< ( ostream&, const T& );
istream& operator>> ( istream&, T& );
부울 부정 연산자 operator! bool operator! ( ) const ; 
이항 부울 연산자 operator&&
operator||
bool operator■ ( const T&, const T& );
배열 인덱스 연산자 operator[] E& operator[ ](size_t) const E& operator [ ](size_t) const ; 
함수 호출 연산자 operator( ) 리턴 타입과 매개변수를 다양하게 지정할 수 있다.
변환 또는 캐스팅 연산자 operator type( ) operator double( ) const ;
메모리 할당, 해제 연산자 operator new 
operator new [ ]
operator delete
operator delete [ ]
void* operator new ( size_t size ) ;
void* operator new[ ] ( size_t size ) ;
void operator delete ( void* ptr ) noexcept ;
void operator delete[ ] ( void* ptr ) noexcept ;
역참조 연산자 operator*
operator->
E& operator* ( ) const ; 
E& operator-> ( ) const ; 

※ 연산자 오버로딩의 한계 

(1). 연산자 기호를 새로 만들 수는 없다. 
(2). 연산자의 평가(evaluation) 순서를 결정하는 우선순위(precedence)와 결합순위(associativity)는 바꿀 수 없다.
(3). 기본 타입 연산자의 의미는 바꿀 수 없다. 오버로딩할 수 있는 연산자는 클래스의 메서드이거나, 오버로딩 하려는 전역 함수의 인수 중 최소 하나가 사용자 정의 타입이어야 한다.다시 말해,  int타입의 +가 뺄셈이 되도록 엉뚱한 의미로 변경할 수 없다.(클래스를 직접 정의할 때는 이렇게 할 수는 있다.) 단, 메모리 할당과 해제 연산자(할당자, 소멸자)는 예외다.프로그램에 나온 모든 메모리 할당에 대한 전역 연산자를 모두 교체할 수 있다. 

 

출처: 전문가를 위한 C++

728x90