-
연산자 오버로딩 유의 사항.effective c++ & c++ 2022. 10. 20. 11:44
=, (), [], ->
1. 위의 4가지 연산자에 대해서는 멤버함수 형태의 오버로딩만 가능하다.
2. 연산자의 순수기능 오버로딩 불가.
int operator+(int &n1,int &n2) { return n1*n2; } //impossible
3. 연산자 오버로딩을 위한 함수의 매개변수는 디폴트 값을 가질 수 없다.
4. endl 연산자 오버로딩
#include <stdio.h> class ostream { public: ostream& operator<<(ostream& (*fp)(ostream& ostm)) { return fp(*this); } }; ostream& endl(ostream& ostm) { ostm << '\n'; fflush(stdout); return ostm; } ostream cout; int main() { cout << endl << endl; //possible return 0; }
#include <stdio.h> class ostream { public: ostream& operator<<(void (*fp)(ostream& ostm)) { fp(*this); return *this; } }; void endl(ostream& ostm) { ostm << '\n'; fflush(stdout); } ostream cout; int main() { cout << endl << endl; //possible return 0; }
'effective c++ & c++' 카테고리의 다른 글
Effective C++ 총 정리본 (0) 2023.08.24 다중 상속 (0) 2022.10.20 virtual 상속 (0) 2022.10.20 c++복습 - 순수 가상 함수와 추상 클래스 그리고 후위/전위 연산자 (0) 2022.10.13 c++ 복습 - 접근 지정자를 동반한 상속의 특성. (0) 2022.10.13