オペレータ


  代入




オブジェクト同士の代入は、デフォルトでメモリがコピーされますので、実装する必要がありません
以下が有効です
  

Integer a,b;
b = 123;
a = b;
a.print();
  
以下は int の代入を実現する為の実装です
  

// *********************************************************
// 代入
// *********************************************************
void Integer::operator = ( int a )
{
	this->nData = a;
}
  
整数をあらわす数字文字列を整数として代入するには以下のコードを実装します
  

// *********************************************************
// 文字列を代入
// *********************************************************
void Integer::operator = ( char *a )
{
	this->nData = atoi( a );
}
  







  足し算




  

// *********************************************************
// 足し算 (1)
// *********************************************************
int Integer::operator + ( int a )
{
	return( this->nData + a );
}
  
このオペレータは以下のコードを実現する為のコードです
  

Integer a,b;
a = 123;
b = a + 27;
b.print();
  
しかし、このままでは以下のコードを実現する事ができません
  

Integer a,b;
a = 123;
b = 27 + a;
b.print();
  
このコードを実現するにはクラス定義に friend キーワードを付けて以下のように定義します

この場合、関数定義がクラススコープでない事に注意して下さい
friend は、引数に自オブジェクト型を使用している必要があります
  

// *********************************************************
// クラス定義
// *********************************************************
class Integer  
{
public:
	Integer();
	virtual ~Integer();
	print();
	// 代入
	void operator = ( int a );
	void operator = ( char *a );
	// 足し算
	int operator + ( int a );
	friend int operator + ( int a, Integer obj );
	int operator + ( Integer obj );
	
	int nData;
};

// *********************************************************
// 足し算 (2)
// *********************************************************
int operator + ( int a, Integer obj )
{
	return( a + obj.nData );
}
  
さらに、オブジェクト同士の足し算ができるように以下のオペーレータを追加します
  

// *********************************************************
// 足し算 (3)
// *********************************************************
int Integer::operator + ( Integer obj )
{
	return( this->nData + obj.nData );
}
  
この他にも定義さえすれば、以下の足し算が可能になります
( クラスのメンバ関数は b = a + "27" で、もう一つは friend となります )
  

Integer a,b;
a = 123;
b = a + "27";
b = "27" + a;
  

  加算代入 +=

  

// *********************************************************
// 加算代入 += (1)
// *********************************************************
int Integer::operator += ( int a )
{
	return( this->nData += a );
}
// *********************************************************
// 加算代入 += (2)
// *********************************************************
int Integer::operator += ( Integer obj )
{
	return( this->nData += obj.nData );
}
// *********************************************************
// 加算代入 += (3)
// *********************************************************
int Integer::operator += ( char *a )
{
	return( this->nData += atoi( a ) );
}
  
以下が実装されます
  

Integer a,b;

// int
a = 123;
a += 27;
a.print();
   
// Integer
a = 123;
b = 27;
a += b;
a.print();

// 文字列
a = 123;
a += "27";
a.print();
  




yahoo  google  MSDN  MSDN(us)  WinFAQ  Win Howto  tohoho  ie_DHTML  vector  wdic  辞書  天気 


[classStyle]
Mozilla/5.0 AppleWebKit/537.36 (KHTML, like Gecko; compatible; ClaudeBot/1.0; +claudebot@anthropic.com)
24/04/20 09:21:15
InfoBoard Version 1.00 : Language=Perl

1 BatchHelper COMprog CommonSpec Cprog CprogBase CprogSAMPLE CprogSTD CprogSTD2 CprogWinsock Cygwin GameScript HTML HTMLcss InstallShield InstallShieldFunc JScript JScriptSAMPLE Jsfuncs LLINK OldProg OracleGold OracleSilver PRO PRObrowser PROc PROconePOINT PROcontrol PROftpclient PROjscript PROmailer PROperl PROperlCHAT PROphp PROphpLesson PROphpLesson2 PROphpLesson3 PROphpfunction PROphpfunctionArray PROphpfunctionMisc PROphpfunctionString PROsql PROvb PROvbFunction PROvbString PROvbdbmtn PROvbonepoint PROwebapp PROwin1POINT PROwinSYSTEM PROwinYOROZU PROwindows ProjectBoard RealPHP ScriptAPP ScriptMaster VBRealtime Vsfuncs a1root access accreq adsi ajax amazon argus asp aspSample aspVarious aspdotnet aw2kinst cappvariety centura ckeyword classStyle cmaterial cmbin cmdbapp cmenum cmlang cmlistbox cmstd cmstdseed cmtxt cs daz3d db dbCommon dbaccess dnettool dos download flex2 flex3 flex4 framemtn framereq freeWorld freesoft gimp ginpro giodownload google hdml home hta htmlDom ie9svg install java javaSwing javascript jetsql jquery jsp jspTest jspVarious lightbox listasp listmsapi listmsie listmsiis listmsnt listmspatch listmsscript listmsvb listmsvc memo ms msde mysql netbeans oraPlsql oracle oracleWiper oraclehelper orafunc other panoramio pear perl personal pgdojo pgdojo_cal pgdojo_holiday pgdojo_idx pgdojo_ref pgdojo_req php phpVarious phpguide plsql postgres ps r205 realC realwebapp regex rgaki ruby rule sboard sc scprint scquest sdb sdbquest seesaa setup sh_Imagick sh_canvas sh_dotnet sh_google sh_tool sh_web shadowbox shgm shjquery shvbs shweb sjscript skadai skywalker smalltech sperl sqlq src systemdoc tcpip tegaki three toolbox twitter typeface usb useXML vb vbdb vbsfunc vbsguide vbsrc vpc wcsignup webanymind webappgen webclass webparts webtool webwsh win8 winofsql wmi work wp youtube