질문 임시 객체의 소멸 타이밍은 언제인가?
열혈강의 C++ 236 page 25라인에 객체가 생성되고 그자리에서 소멸된다.
동적 할당이 따로 없고 따로 포인터로 가르키는 곳도 없어서 바로 소멸이된다.
임시 객체 :
객체의 상수화
#include <iostream> using namespace std;
class SoSimple { private: int num; public: SoSimple(int n): num(n) {} SoSimple&AddNum(int n) { num+=n;
return *this; } void ShowData() const { cout<<"num "<<num<<endl; } }; int main(void) { const SoSimple obj(7); //obj.AddNum(20); // 호환 되지 않는 객체 error obj.ShowData(); return 0; }
AddNum함수는 num의 값이 변화하고 함수에 const가 붙어 있지 않아 호출이 되지 않는다.
클래스와 함수에 대한 friend 선언
- A클래스가 B클레스를 대상으로 friend 선언을 하면, B클래스는 A클래스의 privage맴버에 직접 접근이 가능.
- 단 A클레스도 B클레스의 privage맴버에 직접 접근이 가능하려면 B클레스가 A클레스를 대상으로 friend 선언 해줘야한다.
- friend 선언은 클래스 내에 어디든 위치 할 수 있다 private영역이든 public영역이든 어느영역에 존재하여 도 상관이 없다.
한쪽에서 볼 수 있는 경우
class Boy { private: int height; friend class Girl; public: Boy(int len) : height(len) {} ....... };
양쪽에서 서로 볼 수 있는 경우
#include <iostream> #include <cstring> using namespace std;
class Girl; //Girl 클래스만 선언
class Boy { private:
int height; friend class Girl; public: Boy(int len) : height(len) {} void ShowYourFriendInfo(Girl &frn); };
class Girl { private: char phNum[20]; public: Girl(char* num) { strcpy(phNum,num); } void ShowYourFriendInfo(Boy &frn); friend class Boy; //Boy 클래스에 대한 friend 선언 };
void Boy::ShowYourFriendInfo(Girl &frn) { cout <<"Her phone number: "<<frn.phNum<<endl; }
void Girl::ShowYourFriendInfo(Boy &frn) { cout <<"Her phone number: "<<frn.height<<endl; }
int main() { Boy boy(170); Girl girl ("010-1234-5678"); boy.ShowYourFriendInfo(girl); girl.ShowYourFriendInfo(boy);
return 0;
}
Boy Class의 private에 있는 height변수는 main함수에서 호출이 불가능하다.
하지만 Boy Class에 friend로 main함수를 선언하게 되면 main함수에서도
Boy Class의 private에 있는 height를 출력시킬 수 있다. |
#include <iostream>
using namespace std;
class Point;
class PointOP { private: int opcnt;
public: PointOP() : opcnt(0) {}
Point PointAdd(const Point&, const Point&); Point PointSub(const Point&, const Point&); ~PointOP() { cout <<"Operation times:"<<opcnt<<endl; } };
class Point { private:
int x; int y;
public:
Point(const int &xpos, const int &ypos) : x(xpos),y(ypos) {} friend Point PointOP::PointAdd(const Point&,const Point&); friend Point PointOP::PointSub(const Point&,const Point&); friend void ShowPointPos(const Point&); };
Point PointOP::PointAdd(const Point& pnt1,const Point& pnt2) { opcnt++; return Point(pnt1.x+pnt2.x, pnt1.y+pnt2.y); }
Point PointOP::PointSub(const Point& pnt1,const Point& pnt2) { opcnt++; return Point(pnt1.x-pnt2.x, pnt1.y-pnt2.y); }
int main(void) { Point pos1(1,2); Point pos2(2,4); PointOP op;
ShowPointPos(op.PointAdd(pos1,pos2)); ShowPointPos(op.PointAdd(pos2,pos1));
return 0;
}
void ShowPointPos(const Point& pos) { cout<<"x: "<<pos.x<<","; cout<<"y: "<<pos.y<<endl; } |
static 맴버 변수 (클래스 변수) 특징
|
-static 변수는 일반적인 맴버 변수와 달리 클래스당 하나씩만 생성되기 때문이다.
전역변수와 동일 ( main 함수 호출 이전에 메모리 공간에 올라가서 초기화 )
선언된 클래스의 객체 내에 직접 접근 허용
프로그램내 모든 객체가 공유
#include <iostream> using namespace std;
class smart { public:
static int iNum; int iNum2;
public:
static void test() { iNum=iNum+1; cout<<"Test 실행\n"<<endl; //iNum2=0; // 객체를 생성하지 않아서 error }
}; int smart::iNum=100;
int main() { cout<<smart::iNum<<endl; smart::test();
cout<<smart::iNum<<endl; return 0; } |
int iNum2 static 변수가 아니다. 그렇기때문에 static함수로 선언되었는 곳에서 static이 아닌것은
사용이 불가능하다.
- 객체의 맴버가 아니므로 맴버 변수에 접근이 불가.
- 어느 맴버변수에 접근 할지가 불분명
- 객체생성이전에도 호출은 가능하지만 맴버변수에 어떻게 접근 할 것인가?
이세가지 이유에 따라 static 맴버함수 내에서는 static으로 선언되지 않은 맴버변수는 접근 맴버함수의 호출도 불가능하다.
"Static 맴버함수 내에서는 Static맴버변수와 Static 맴버함수만 호출이 가능하다" // 열혈강의 C++ 263 밑에 부분 참조
RFID
hComm=CreateFile(TEXT("COM3"),GENERIC_READ | GENERIC_WRITE ,0,NULL,OPEN_EXISTING ,FILE_ATTRIBUTE_NORMAL,0); if(INVALID_HANDLE_VALUE ==hComm) { MessageBox(hnd,TEXT("포트열 수 없음 \n"),TEXT("Menu DEmo"),MB_OK); return; } if(0==SetupComm(hComm,4096,3096)) { MessageBox(hnd,TEXT("버퍼 설정 에러\n"),TEXT("Menu DEmo"),MB_OK); CloseHandle(hComm); return; } if(0==PurgeComm(hComm,PURGE_TXABORT|PURGE_TXCLEAR)) { MessageBox(hnd,TEXT("버퍼 초기화 에러\n"),TEXT("Menu DEmo"),MB_OK); CloseHandle(hComm); return; } sPState.DCBlength=sizeof(sPState);
if(0==GetCommState(hComm,&sPState)) { MessageBox(hnd,TEXT("시리얼 상태 읽기 에러\n"),TEXT("Menu DEmo"),MB_OK); CloseHandle(hComm); return; } sPState.BaudRate =CBR_38400; sPState.ByteSize= 8; sPState.Parity=EVENPARITY; sPState.StopBits=ONESTOPBIT;
cTime.ReadIntervalTimeout=MAXDWORD; cTime.ReadTotalTimeoutMultiplier=0; cTime.WriteTotalTimeoutConstant=0; cTime.WriteTotalTimeoutMultiplier=0; cTime.WriteTotalTimeoutConstant=0;
SetCommTimeouts(hComm,&cTime); if(0==SetCommState(hComm,&sPState)) { MessageBox(hnd,TEXT("시리얼 상태 설정 에러\n"),TEXT("Menu DEmo"),MB_OK); CloseHandle(hComm); return; } BportONOFF=TRUE ;// 포트열기 완료 } |
메모장에 printf문으로 작성 된 부분을 모두 MessageBox로 교체
시리얼PAD에서 문자를 적으면 WindowAPI에 16진수로 변환되어 표현이 되도록 해준다.
결과
이상으로 필기 마치도록 하겠습니다.
늦어서 죄송합니다