|
|
// *********************************************************
// クラス定義
// *********************************************************
class MyString
{
public:
MyString();
virtual ~MyString();
void operator = ( LPTSTR lpString );
LPTSTR lpBuff;
};
// *********************************************************
// 代入オペレータ
// *********************************************************
void MyString::operator = ( LPTSTR lpString )
{
lstrcpy( lpBuff, lpString );
}
| |
|
|
|
|
int main(int argc, char* argv[])
{
MyString a;
a = "ABC";
printf("%s\n", a.lpBuff );
return 0;
}
| |
|
|
|
|
// *********************************************************
// オブジェクト複写
// *********************************************************
void MyString::operator = ( MyString &obj )
{
lstrcpy( lpBuff, obj.lpBuff );
}
| |
|
MyString a;
MyString *x = new MyString();
*x = "abc";
a = x;
printf( "%s\n", a.lpBuff );
delete x;
|
MyString a;
MyString *x = new MyString();
x->operator = ( "abc" );
a.operator = ( x );
printf( "%s\n", a.lpBuff );
delete x;
|
|
// *********************************************************
// オブジェクトのポインタより複写
// *********************************************************
void MyString::operator = ( MyString *obj )
{
lstrcpy( lpBuff, obj->lpBuff );
}
| |
|
|
|
|
// *********************************************************
// 整数複写
// *********************************************************
void MyString::operator = ( int nData )
{
wsprintf( lpBuff, "%ld", nData );
}
| |
|
|
|
|
#include <comdef.h>
// *********************************************************
// 実数複写
// *********************************************************
void MyString::operator = ( double nData )
{
_variant_t vWork = nData;
lstrcpy( lpBuff, (LPTSTR)(_bstr_t)vWork );
}
| |
|
|
|
enum MyEnum {
mysSysDate = 1,
mysYear = 2,
mysMonth = 3,
mysDay = 4
};
|
// *********************************************************
// 列挙定数複写
// *********************************************************
void MyString::operator = ( MyEnum nType )
{
switch( nType ) {
case mysSysDate:
{
SYSTEMTIME st;
GetLocalTime( &st );
wsprintf(
lpBuff,
"%04d/%02d/%02d",
st.wYear,
st.wMonth,
st.wDay
);
}
break;
}
}
| |
|

|
|