类与类之间的转化,重载user-defined类型运算符

#include <new.h> #include <iostream> using namespace std; #include <stdlib.h> #include <time.h> #include <stdio.h> class d_Hello; /** * probeMatch message format in the received data soap:body. */ class d_probeMatch { public: d_probeMatch() { memset(m_d_Types,0,50); memset(m_d_Scopes,0,500); memset(m_d_XAddrs,0,50); memset(m_d_MetadataVersion,0,10); } d_probeMatch(const d_probeMatch &probeObj) { strcpy(m_d_Types,probeObj.m_d_Types); strcpy(m_d_Scopes,probeObj.m_d_Scopes); strcpy(m_d_XAddrs,probeObj.m_d_XAddrs); strcpy(m_d_MetadataVersion,probeObj.m_d_MetadataVersion); } // d_probeMatch(const d_Hello &helloObj) // { // strcpy(m_d_Types,helloObj.m_d_Types); // strcpy(m_d_Scopes,helloObj.m_d_Scopes); // strcpy(m_d_XAddrs,helloObj.m_d_XAddrs); // strcpy(m_d_MetadataVersion,helloObj.m_d_MetadataVersion); // } d_probeMatch & operator=(const d_probeMatch &probeObj) { strcpy(m_d_Types,probeObj.m_d_Types); strcpy(m_d_Scopes,probeObj.m_d_Scopes); strcpy(m_d_XAddrs,probeObj.m_d_XAddrs); strcpy(m_d_MetadataVersion,probeObj.m_d_MetadataVersion); return *this; } // d_probeMatch &operator=(const d_Hello &helloObj) // { // strcpy(m_d_Types,helloObj.m_d_Types); // strcpy(m_d_Scopes,helloObj.m_d_Scopes); // strcpy(m_d_XAddrs,helloObj.m_d_XAddrs); // strcpy(m_d_MetadataVersion,helloObj.m_d_MetadataVersion); // return *this; // } virtual ~d_probeMatch(){} char m_d_Types[50]; char m_d_Scopes[500]; char m_d_XAddrs[50]; char m_d_MetadataVersion[10]; }; /** * Hello message format. */ class d_Hello { public: d_Hello() { memset(m_d_Types,0,50); memset(m_d_Scopes,0,500); memset(m_d_XAddrs,0,50); memset(m_d_MetadataVersion,0,10); } virtual ~d_Hello(){} d_Hello(const d_Hello &helloObj) { strcpy(m_d_Types,helloObj.m_d_Types); strcpy(m_d_Scopes,helloObj.m_d_Scopes); strcpy(m_d_XAddrs,helloObj.m_d_XAddrs); strcpy(m_d_MetadataVersion,helloObj.m_d_MetadataVersion); } operator d_probeMatch() { d_probeMatch probe; strcpy(probe.m_d_Types,m_d_Types); strcpy(probe.m_d_Scopes,m_d_Scopes); strcpy(probe.m_d_XAddrs,m_d_XAddrs); strcpy(probe.m_d_MetadataVersion,m_d_MetadataVersion); return probe; } //attributes char m_d_Types[50]; char m_d_Scopes[500]; char m_d_XAddrs[50]; char m_d_MetadataVersion[10]; }; void main() { d_Hello hello; strcpy(hello.m_d_MetadataVersion,"version1"); strcpy(hello.m_d_Scopes,"scopes"); strcpy(hello.m_d_Types,"types"); strcpy(hello.m_d_XAddrs,"address"); d_probeMatch probe = hello; printf("%s/n",probe.m_d_Scopes); getchar(); } 

在operator d_probeMatch和d_probeMatch(const d_probeMatch &probeObj)添加断点,

会发现先调用operator d_probeMatch()这个函数,再调用拷贝构造(第一次是返回在operator d_probeMatch

中的临时对象调用,第二次才是d_probeMatch probe = hello;初始化probe对象)。

 

若在main中修改代码如下:

d_probeMatch probe ; probe = hello; 

择第一次调用拷贝构造后,第二次调用的是d_probeMatch & operator=(const d_probeMatch &probeObj)

赋值运算 。

当然为了提高效率可以直接调用d_probeMatch(const d_Hello &helloObj),类的生命顺序调换一下就好了。

你可能感兴趣的:(SOAP,include,Types)