C++ 초보인 사람입니다.
sting class 에서 "+" 연산자를 override 해서 append 가 가능한 것으로 알고 있는데요.
"문자열" + string 인 경우에도 연산자가 뒤에 있지 않고 앞에 있어도
알아서 "+" 연산자가 호출되는 건가요?
자기 뒤에 있는 연산자로써만 의미가 있는 것 아닌가요?
제가 초보라서 연산자가 어떤 식으로 동작하는지 햇갈려서 질문드립니다.
조언 주시면 감사하겠습니다.
string 클래스의 operator+는 다음과 같이 비멤버 함수로 구현되어 있습니다.
string operator+(const char* lhs, const string& rhs); string operator+(const string& lhs, const char* rhs);
따라서 문자열과 string의 순서와 상관없이 + 연산자가 동작하게 됩니다.
덧: overriding이 아니라 overloading입니다.
텍스트 포맷에 대한 자세한 정보
<code>
<blockcode>
<apache>
<applescript>
<autoconf>
<awk>
<bash>
<c>
<cpp>
<css>
<diff>
<drupal5>
<drupal6>
<gdb>
<html>
<html5>
<java>
<javascript>
<ldif>
<lua>
<make>
<mysql>
<perl>
<perl6>
<php>
<pgsql>
<proftpd>
<python>
<reg>
<spec>
<ruby>
<foo>
[foo]
string 클래스의 operator+는 다음과 같이
string 클래스의 operator+는 다음과 같이 비멤버 함수로 구현되어 있습니다.
string operator+(const char* lhs, const string& rhs);
string operator+(const string& lhs, const char* rhs);
따라서 문자열과 string의 순서와 상관없이 + 연산자가 동작하게 됩니다.
덧: overriding이 아니라 overloading입니다.
댓글 달기