#include <iostream>
namespace mystd
{
using namespace std;
class ostream
{
public:
ostream& operator<< (char * str)
{
printf("%s", str);
return *this;
}
ostream& operator<< (char str)
{
printf("%c", str);
return *this;
}
ostream& operator<< (int num)
{
printf("%d", num);
return *this;
}
ostream& operator<< (double e)
{
printf("%g", e);
return *this;
}
ostream& operator<< (ostream& (*fp)(ostream &ostm))
{
return fp(*this);
}
};
ostream& endl(ostream &ostm)
{
ostm<<'\n';
fflush(stdout);
return ostm;
}
ostream cout;
}
int main(void)
{
using mystd::cout;
using mystd::endl;
cout<<3.14<<endl<<123<<endl;
return 0;
}
(10~29열) cout 객체의 참조값을 반환하는 형태로 확장.
(36, 40열) endl 함수는 인자로 전달된 객체의 참조값을 반환
반환된 값을 재 반환하는 형태로 연산자 오버로딩.
#include <iostream>
using namespace std;
class Point
{
private:
int xpos, ypos;
public:
Point(int x=0, int y=0) : xpos(x), ypos(y)
{ }
void ShowPosition() const
{
cout<<'['<<xpos<<", "<<ypos<<']'<<endl;
}
friend ostream& operator<<(ostream&, const Point&);
};
ostream& operator<<(ostream& os, const Point& pos)
{
os<<'['<<pos.xpos<<", "<<pos.ypos<<']'<<endl;
return os;
}
int main(void)
{
Point pos1(1, 3);
cout<<pos1;
Point pos2(101, 303);
cout<<pos2;
return 0;
}
(20열) 인자로 전달된 cout의 참조자를 통한 출력구성
(27,29열) 이 두 열로 인해서 18행에 정의된 함수호출
<<연산자의 오른편에 Point 객체이기 때문
#include <iostream>
#include <cstring>
using namespace std;
class Smart
{
public:
char * cp;
public:
Smart()
{
cp=new char[30];
strcpy(cp,"Smart");
}
~Smart()
{
getchar();
delete []cp;
}
Smart& operator= (Smart& R)
{
int iCnt;
iCnt=strlen(R.cp);
if(0!=cp)
{
delete []cp;//
}
cp=new char[iCnt+1];
strcpy(cp,R.cp);
return * this;
}
};
int main(void)
{
Smart obj1;
cout<<obj1.cp<<endl;
Smart obj2;
obj1=obj2;
return 0;
}
(30열) 메모리 누수를 막기 위한 메모리 해제연산
SetWindowText(hstatic[4],buff);//다들어 오면 00 이므로 안씀
void HostComm(void)
{
SetWindowText(hstatic[3],TEXT(" CARD-TYPE "));
ShowWindow(hstatic[3],SW_SHOW);
switch(ucpParam[0])
{
case ISO_STANDARD:
SetWindowText(hstatic[5],TEXT("ISO_STANDARD"));
ShowWindow(hstatic[5],SW_SHOW);
break;
case I_CODE_EPC1:
SetWindowText(hstatic[5],TEXT("I_CODE_EPC1"));
ShowWindow(hstatic[5],SW_SHOW);
break;
case I_CODE_EPC2:
SetWindowText(hstatic[5],TEXT("I_CODE_EPC2"));
ShowWindow(hstatic[5],SW_SHOW);
break;
case I_CODE_UID:
SetWindowText(hstatic[5],TEXT("I_CODE_UID"));
ShowWindow(hstatic[5],SW_SHOW);
break;
default:
SetWindowText(hstatic[5],TEXT("UNKNOWN-TYPE"));
ShowWindow(hstatic[5],SW_SHOW);
return;
}
SetWindowText(hstatic[6],TEXT("RF_TYPE"));
ShowWindow(hstatic[6],SW_SHOW);
switch(ucpParam[5]&MASK_RF_TEC)
{
case _13_56MHZ:
SetWindowText(hstatic[7],TEXT("13_56MHZ"));
ShowWindow(hstatic[7],SW_SHOW);
break;
case UHF_TRANSPONDER:
SetWindowText(hstatic[7],TEXT("UHF_TRANSPONDER"));
ShowWindow(hstatic[7],SW_SHOW);
break;
default:
SetWindowText(hstatic[7],TEXT("UNKNOW-TYPE"));
ShowWindow(hstatic[7],SW_SHOW);
return;
}
SetWindowText(hstatic[9],TEXT("TYPE_NO"));
ShowWindow(hstatic[9],SW_SHOW);
switch(ucpParam[5]&MASK_TYPE_NO)
{
case PHILIPS_I_CODE_1:
SetWindowText(hstatic[8],TEXT("PHILIPS_I_Code_1"));
ShowWindow(hstatic[8],SW_SHOW);
break;
case TI_TAG_IT:
SetWindowText(hstatic[8],TEXT("TI_TAG_IT"));
ShowWindow(hstatic[8],SW_SHOW);
break;
case ISO15693_TAGS:
SetWindowText(hstatic[8],TEXT("ISO15693_TAGS"));
ShowWindow(hstatic[8],SW_SHOW);
break;
case PHILIPS_I_CODE_EPC:
SetWindowText(hstatic[8],TEXT("PHILIPS_I_CODE_EPC"));
ShowWindow(hstatic[8],SW_SHOW);
break;
case PHILIPS_I_CODE_UID:
SetWindowText(hstatic[8],TEXT("PHILIPS_I_CODE_UID"));
ShowWindow(hstatic[8],SW_SHOW);
break;
default:
SetWindowText(hstatic[8],TEXT("UNKNOWN TYPE"));
ShowWindow(hstatic[8],SW_SHOW);
return;
}
}