#include <new.h><br />#include <iostream><br />using namespace std;<br />#include <stdlib.h><br />#include <time.h><br />#include <stdio.h><br />class d_Hello;<br />/**<br />* probeMatch message format in the received data soap:body.<br />*/<br />class d_probeMatch<br />{<br />public:<br />d_probeMatch()<br />{<br />memset(m_d_Types,0,50);<br />memset(m_d_Scopes,0,500);<br />memset(m_d_XAddrs,0,50);<br />memset(m_d_MetadataVersion,0,10);<br />}<br />d_probeMatch(const d_probeMatch &probeObj)<br />{<br />strcpy(m_d_Types,probeObj.m_d_Types);<br />strcpy(m_d_Scopes,probeObj.m_d_Scopes);<br />strcpy(m_d_XAddrs,probeObj.m_d_XAddrs);<br />strcpy(m_d_MetadataVersion,probeObj.m_d_MetadataVersion);<br />}<br />// d_probeMatch(const d_Hello &helloObj)<br />// {<br />// strcpy(m_d_Types,helloObj.m_d_Types);<br />// strcpy(m_d_Scopes,helloObj.m_d_Scopes);<br />// strcpy(m_d_XAddrs,helloObj.m_d_XAddrs);<br />// strcpy(m_d_MetadataVersion,helloObj.m_d_MetadataVersion);<br />// }<br />d_probeMatch & operator=(const d_probeMatch &probeObj)<br />{<br />strcpy(m_d_Types,probeObj.m_d_Types);<br />strcpy(m_d_Scopes,probeObj.m_d_Scopes);<br />strcpy(m_d_XAddrs,probeObj.m_d_XAddrs);<br />strcpy(m_d_MetadataVersion,probeObj.m_d_MetadataVersion);<br />return *this;<br />}<br />// d_probeMatch &operator=(const d_Hello &helloObj)<br />// {<br />// strcpy(m_d_Types,helloObj.m_d_Types);<br />// strcpy(m_d_Scopes,helloObj.m_d_Scopes);<br />// strcpy(m_d_XAddrs,helloObj.m_d_XAddrs);<br />// strcpy(m_d_MetadataVersion,helloObj.m_d_MetadataVersion);<br />// return *this;<br />// }<br />virtual ~d_probeMatch(){}</p><p>char m_d_Types[50];<br />char m_d_Scopes[500];<br />char m_d_XAddrs[50];<br />char m_d_MetadataVersion[10];<br />};<br />/**<br />* Hello message format.<br />*/<br />class d_Hello<br />{<br />public:<br />d_Hello()<br />{<br />memset(m_d_Types,0,50);<br />memset(m_d_Scopes,0,500);<br />memset(m_d_XAddrs,0,50);<br />memset(m_d_MetadataVersion,0,10);<br />}<br />virtual ~d_Hello(){}<br />d_Hello(const d_Hello &helloObj)<br />{<br />strcpy(m_d_Types,helloObj.m_d_Types);<br />strcpy(m_d_Scopes,helloObj.m_d_Scopes);<br />strcpy(m_d_XAddrs,helloObj.m_d_XAddrs);<br />strcpy(m_d_MetadataVersion,helloObj.m_d_MetadataVersion);<br />}</p><p>operator d_probeMatch()<br />{<br />d_probeMatch probe;<br />strcpy(probe.m_d_Types,m_d_Types);<br />strcpy(probe.m_d_Scopes,m_d_Scopes);<br />strcpy(probe.m_d_XAddrs,m_d_XAddrs);<br />strcpy(probe.m_d_MetadataVersion,m_d_MetadataVersion);<br />return probe;<br />}<br />//attributes<br />char m_d_Types[50];<br />char m_d_Scopes[500];<br />char m_d_XAddrs[50];<br />char m_d_MetadataVersion[10];<br />};<br />void main()<br />{<br />d_Hello hello;<br />strcpy(hello.m_d_MetadataVersion,"version1");<br />strcpy(hello.m_d_Scopes,"scopes");<br />strcpy(hello.m_d_Types,"types");<br />strcpy(hello.m_d_XAddrs,"address");<br />d_probeMatch probe = hello;<br />printf("%s/n",probe.m_d_Scopes);<br />getchar();<br />}
在operator d_probeMatch和d_probeMatch(const d_probeMatch &probeObj)添加斷點,
會發現先調用operator d_probeMatch()這個函數,再調用拷貝構造(第一次是返回在operator d_probeMatch
中的臨時對象調用,第二次才是d_probeMatch probe = hello;初始化probe對象)。
若在main中修改代碼如下:
d_probeMatch probe ;<br />probe = hello;
擇第一次調用拷貝構造後,第二次調用的是d_probeMatch & operator=(const d_probeMatch &probeObj)
賦值運算 。
當然為了提高效率可以直接調用d_probeMatch(const d_Hello &helloObj),類的生命順序調換一下就好了。