게시판 즐겨찾기
편집
드래그 앤 드롭으로
즐겨찾기 아이콘 위치 수정이 가능합니다.
클래스의 상수형(const) 객체는 상수형 멤버 함수만 호출 가능한가요?
게시물ID : programmer_10272짧은주소 복사하기
작성자 : 우와우와우왕
추천 : 0
조회수 : 523회
댓글수 : 10개
등록시간 : 2015/05/20 15:44:33
 
#include<iostream>
using namespace std;
class Cat
{
        public:
               Cat() :itsAge(0),itsWeight(0){}
               Cat(int age, int weight) :itsAge(age), itsWeight(weight){}
 
               int GetAge()const{ return itsAge; }
               int GetWeight(){ return itsWeight; }
 
               void operator+(const Cat &);
               friend Cat operator+(const Cat &, const Cat &);
 
        private:
               int itsAge;
               int itsWeight;
};
 
Cat operator+(const Cat & lhs, const Cat & rhs)
{
        Cat temp;
        int totalAge = lhs.GetAge() + rhs.GetAge();
        int totalweight = lhs.GetWeight() + rhs.GetWeight();
        return temp;
}
 
디버그를 할때에 노란색친 두 부분에 대해서 아래와 같은 에러가 나오네요
 
error C2662: 'int Cat::GetAge(void)' : cannot convert 'this' pointer from 'const Cat' to 'Cat &'
1>          Conversion loses qualifiers
 
operator+에서 두 매개변수에 '&'를 빼서 참조자가 아니게 해도 결과는 똑같구요
 
'아무래도 상수형 객체인 lhs와 rhs가 GetWeight()를 호출할려고 할때 
GetWeight는 비상수형 멤버 함수가 아니기 때문에 호출할수가 없다.
 
즉, 클래스의 상수형 객체는 상수형 멤버 함수만 호출할수있다.'
 
 
로 해석해야 되는것 같은데... 맞나요??
책을 아무리 뒤져봐도 이런 상황은 안나오네요 ㅠㅠ
전체 추천리스트 보기
새로운 댓글이 없습니다.
새로운 댓글 확인하기
글쓰기
◀뒤로가기
PC버전
맨위로▲
공지 운영 자료창고 청소년보호