zoukankan      html  css  js  c++  java
  • c++ 逗号操作符重载

    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;
    }

    输出信息



  • 相关阅读:
    Java基础
    数据库表设计
    Spring循环依赖
    Mysql类型转换
    Mysql刷题
    JavaScript
    Git
    告别.NET生成报表统计图的烦恼
    JS给页面标签添加事件(或超链接链接)
    发现联想手机P630型号的一个严重的系统Bug
  • 原文地址:https://www.cnblogs.com/yangykaifa/p/7269428.html
Copyright © 2011-2022 走看看