Java教程

std::ostream & operator<<

本文主要是介绍std::ostream & operator<<,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
#include <iostream>
#include <iostream>
#include <string>

class test{
public:
    test(const std::string _domain,const std::string _interface,const std::string _instance);
private:

    std::string domain_;
    std::string interface_;
    std::string instance_;
   friend  std::ostream &operator<<(std::ostream &_out, const test &_address);
};

test::test(const std::string _domain,const std::string _interface,const std::string _instance) {
    // TODO: handle error situation (_address is no valid CommonAPI address)
    domain_ = _domain;
    interface_ = _interface;
    instance_ = _instance;
}

std::ostream &
operator<<(std::ostream &_out, const test &_address) {
    _out << _address.domain_
          << ":" << _address.interface_
          << ":" << _address.instance_;
    return _out;
}

int main(){
    test t("a","b","c");
    std::cout<<t<<std::endl;
    return 0;
}

 

ygy@ygy-VirtualBox:~/work/tmp$ g++ b.cpp 
ygy@ygy-VirtualBox:~/work/tmp$ ./a.out 
a:b:c

  

这篇关于std::ostream & operator<<的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!