c++ 逗号操作符重载

来源:互联网 发布:ios电话拦截软件 编辑:程序博客网 时间:2024/05/20 04:49
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 \n";    return 0;}

输出信息



0 0
原创粉丝点击