zoukankan      html  css  js  c++  java
  • C++ class外的 >> 重载,输入流,重载示例。不应该定义类内的>>重载

    #include <iostream>
    
    // overloading "operator >> " outside class
    // >> 应该定义在类之外。
    
    //////////////////////////////////////////////////////////
    
    class Rectangle
    {
    public:
    	Rectangle(int w, int h)
    		: width(w), height(h)
    	{};
    
    	~Rectangle() {};
    
    public:
    	int width;
    	int height;
    };
    
    
    //////////////////////////////////////////////////////////
    
    std::istream&
    operator>> (std::istream& is, Rectangle& rec)
    {
    	is >> rec.width >> rec.height;
    	return  is;
    
    }
    
    std::ostream&
    operator<< (std::ostream& os, const Rectangle& rec) // 这里的 const 是必要的,rec应该是一个常量,请注意
    {
    	os << rec.width << ", " << rec.height;
    	return  os;
    
    }
    
    //////////////////////////////////////////////////////////
    
    int main()
    {
    	Rectangle a(40, 10);
    	std::cin >> a;
    
    	std::cout
    		<< "a = " << a << std::endl;
    
    	return 0;
    }
    

      

  • 相关阅读:
    join
    runlevel 运行级别
    腾讯一shell试题.
    awk grep sed 的一些问题
    while read line do done < file
    awk 中 RS,ORS,FS,OFS 区别与联系
    节选
    rpm -qa -qc 查询安装过的软件
    css实现两端对齐
    JS表单验证
  • 原文地址:https://www.cnblogs.com/alexYuin/p/11966445.html
Copyright © 2011-2022 走看看