[16:00]함수가 const인지 아닌지로 overloading을 하는 게 맞나요? +코드 오류 질문
276
작성한 질문수 7
#include <iostream>
#include <string>
using namespace std;
class Some {
public:
string _val = "default";
const string& getVal() const {
cout << "const version" << endl;
return _val;
}
string& getVal() {
cout << "non-const version" << endl;
return _val;
}
};
int main() {
Some some;
some.getVal();
const Some some2;
some2.getVal();
return 0;
}
[질문 1]
some.getVal()과 some2.getVal()같이 오버로딩이 가능한 것이,
함수가 const인지 아닌지로 구분을 했기 때문인지
아니면 return type이 const인지 아닌지로 구분을 했기 때문인지 궁금합니다.
[질문 2]
return type으로는 오버로딩을 할 수 없기 때문에 질문 1의 답은 전자라고 추측하고 확인을 하기 위해 코드를 아래와 같이 바꿔 봤습니다.(7번째 줄을 const string& getVal() const에서 string& getVal() const 로 바꿔 본 것밖에 없습니다.)
#include <iostream>
#include <string>
using namespace std;
class Some {
public:
string _val = "default";
string& getVal() const {
cout << "const version" << endl;
return _val;
}
string& getVal() {
cout << "non-const version" << endl;
return _val;
}
};
int main() {
Some some;
some.getVal();
const Some some2;
some2.getVal();
return 0;
}
다음과 같이 바꿨을 때 string& getVal() const cout << "const version" << endl;
return _val;
}
의 return _val부분에서 'qualifiers dropped in binding reference of type "std::strung &" to initialer of type "const std::string" '이라는 오류가 났습니다.
오류의 원인이 궁금합니다.
답변 1
0
[질문 1]
some.getVal()과 some2.getVal()같이 오버로딩이 가능한 것이,
함수가 const인지 아닌지로 구분을 했기 때문인지
아니면 return type이 const인지 아닌지로 구분을 했기 때문인지 궁금합니다.
>> 전자가 맞습니다.
가령 아래의 코드는 잘 컴파일 됩니다.
#include <iostream>
using namespace std;
class Test {
protected:
int x;
public:
Test(int i)
: x(i)
{
}
void fun() const
{
cout << "fun() const called " << endl;
}
void fun() { cout << "fun() called " << endl; }
};
int main()
{
Test t1(10);
const Test t2(20);
t1.fun();
t2.fun();
return 0;
}[질문 2]
첨부하신 코드에 대한 오류 이유
>> const 를 사용하는 이유는 해당 함수가 속해있는 클래스의 멤버변수를 바꾸지 않기 위함입니다.
그런데 멤버변수 레퍼런스를 리턴해버리면 변경이 가능해집니다. 그래서 발생한 오류입니다.
요컨대 아래 코드는 error가 없습니다.
#include <iostream>
#include <string>
using namespace std;
class Some {
public:
string _val = "default";
string getVal() const {
cout << "const version" << endl;
return _val;
}
string & getVal() {
cout << "non-const version" << endl;
return _val;
}
};
int main() {
Some some;
some.getVal();
const Some some2;
some2.getVal();
return 0;
}
변수가 메모리에 저장되는 것을 알려주는 강의가 어떤강의였죠
1
483
1
메모리 주소 10진수로 출력
1
673
1
클래스 템플릿 특수화에서 boolalpha로 표현된 리턴값에 대해 질문이 있습니다.
1
520
1
여러가지 리턴 타입에 관한 강의가 어떤 걸까요?
1
550
1
메모리 주소에 관한 질분
0
689
1
인터페이스 클래스에서 reportError의 매개변수에 대해 궁금한 것이 있습니다.
0
558
1
형변환 오버로딩에서 const 관련 질문이 있습니다.
0
451
1
Digit 뒤에 reference를 사용하는 이유
0
516
1
4.2 전역 변수, 정적 변수, 내부 연결, 외부 연결
0
332
1
dat파일이...
0
544
1
TODO:대입 연산자 오버로딩에 대한 소스코드입니다.
0
654
1
복사 생성자 관련 질문이 있습니다.
0
459
1
수업 중 궁금한점이 있습니다.
1
395
1
라이브러리자체가 이해가 되지 않습니다.
0
570
1
마지막 예제 질문
0
308
1
증감연산자 위치에 따른 수행 순서 질문입니다.
0
384
1
단항 연산자 오버로딩에서 return 부분에 질문이 있습니다.
1
421
1
friend함수 관련 질문이 있습니다.
0
317
1
operator+ 정의부분에서 궁금한 것이 있습니다.
0
453
1
3분 17초 질문
0
358
1
함수에 값을 대입한다는 개념이 이해가 되지 않습니다.
0
454
1
int getvalue() const에서 const는 왜 뒤에 붙는건가요?
0
457
2
const Something &st에서 const를 빼면 안되나요?
0
307
1
friend함수는 다른 클래스의 멤버함수로 쓸 수 없나요??
1
499
1





