[00:39] 형변환을 오버로딩할 때는 리턴타입은 지정을 안해주나요?
194
작성한 질문수 85
안녕하세요.
operator int()
{
cout << "cast here" << endl;
return m_cents;
}
이렇게 해주려고 하니까
C++ return type may not be specified on a conversion function
int() 요게 컨버전 함수인가요?
감사합니다.
===자답====
답을 찾았습니다.
주소: https://www.learncpp.com/cpp-tutorial/910-overloading-typecasts/
There are two things to note:
- To overload the function that casts our class to an int, we write a new function in our class called operator int(). Note that there is a space between the word operator and the type we are casting to.
- User-defined conversions do not take parameters, as there is no way to pass arguments to them.
- User-defined conversions do not have a return type. C++ assumes you will be returning the correct type.
그리고 여기 있는 내용을 보다가 궁금한 게 생겼는데요.
User-defined conversions allow us to convert our class into another data type. Take a look at the following class:
This class is pretty simple: it holds some number of cents as an integer, and provides access functions to get and set the number of cents.
If we can convert an int into a Cents, then doesn’t it also make sense for us to be able to convert a Cents back into an int? In some cases, this might not be true, but in this case, it does make sense.
int를 Cents로 변환하는 생성자를 제공한다.
이 개념은 또 처음이라서요 (제가 놓쳤을 수도 있습니다)
저는 그냥 생성자에서 클래스의 멤버 변수를 초기화만 해주고 있다고 생각했었어요.
이건 어떻게 봐야할까요?
감사합니다.
답변 2
변수가 메모리에 저장되는 것을 알려주는 강의가 어떤강의였죠
1
466
1
메모리 주소 10진수로 출력
1
653
1
클래스 템플릿 특수화에서 boolalpha로 표현된 리턴값에 대해 질문이 있습니다.
1
499
1
여러가지 리턴 타입에 관한 강의가 어떤 걸까요?
1
534
1
메모리 주소에 관한 질분
0
679
1
인터페이스 클래스에서 reportError의 매개변수에 대해 궁금한 것이 있습니다.
0
549
1
형변환 오버로딩에서 const 관련 질문이 있습니다.
0
443
1
Digit 뒤에 reference를 사용하는 이유
0
510
1
4.2 전역 변수, 정적 변수, 내부 연결, 외부 연결
0
323
1
dat파일이...
0
539
1
TODO:대입 연산자 오버로딩에 대한 소스코드입니다.
0
644
1
복사 생성자 관련 질문이 있습니다.
0
454
1
수업 중 궁금한점이 있습니다.
1
390
1
라이브러리자체가 이해가 되지 않습니다.
0
561
1
마지막 예제 질문
0
302
1
증감연산자 위치에 따른 수행 순서 질문입니다.
0
375
1
단항 연산자 오버로딩에서 return 부분에 질문이 있습니다.
1
411
1
friend함수 관련 질문이 있습니다.
0
312
1
operator+ 정의부분에서 궁금한 것이 있습니다.
0
447
1
3분 17초 질문
0
350
1
함수에 값을 대입한다는 개념이 이해가 되지 않습니다.
0
448
1
int getvalue() const에서 const는 왜 뒤에 붙는건가요?
0
445
2
const Something &st에서 const를 빼면 안되나요?
0
300
1
friend함수는 다른 클래스의 멤버함수로 쓸 수 없나요??
1
493
1





