Overload Operator Comma
首先看看think in c++ 给出的一个重载的样例
#include <iostream> using namespace std; class After { public: const After& operator,(const After&) const { cout << "After::operator,()" << endl; return *this; } }; class Before {}; Before& operator,(int, Before& b) { cout << "Before::operator,()" << endl; return b; } int main() { After a, b; a, b; // Operator comma called Before c; 1, c; // Operator comma called } ///:~
以下是实际使用中用到的样例
#include <iostream> #include <typeinfo> using namespace std; class CClient { public: CClient(){}; ~CClient(){}; public: CClient& operator,(string str) { strIpAddr_=str; return *this; } CClient& operator,(int nVal) { nPort_=nVal; return *this; }; bool connect() { //Connect(strIpAddr_,nPort_); cout<<"connect to server "<<endl; return true; } public: string strIpAddr_; int nPort_; }; struct OutputDebugInfo { OutputDebugInfo& operator,(string str) { cout<<str; } }; #define outputDebugInfo OutputDebugInfo{}, int main() { (CClient {},80,"192.168.1.10").connect(); outputDebugInfo "Log: this is Debug Infomation Test "; return 0; }
输出信息