본문 바로가기
01.Bit 단기/C++

24_상수멤버함수

by chojju 2018. 5. 3.
반응형

#include <iostream>
using namespace std;
//상수화 맴버
class MyMath
{
 int num;
public:
 //상수맴버함수
 //맴버변수 값을 변경하지 않겠다..
 //일반맴버함수의 호출도 불가한다.
 //why? 일반맴버함수가 맴버변수값 수정할수있기때문
 void foo(int n) const
 {
//  num++; //error
  n++; //ok.
//  woo(); //error.
 }
 void woo()
 {
  num++; //맴버변수값 수정..접근 가능
 }

};

int main()
{
 MyMath m;

 //생성과 동시에 초기화
 int num = 0;
 //생성후 대입연산을 이용한 초기화
 int num1;
 num1 = 0;
 return 0;
}

반응형

'01.Bit 단기 > C++' 카테고리의 다른 글

26_has a 객체초기화  (0) 2018.05.03
25_has a 객체생성순서  (0) 2018.05.03
23_상수멤버변수  (0) 2018.05.03
22_static 멤버함수  (0) 2018.05.03
21_static 멤버변수  (0) 2018.05.03

댓글