zoukankan      html  css  js  c++  java
  • C++ class 内的 () 重载示例

    #include <iostream>
    
    // overloading "operator () " outside class
    
    //////////////////////////////////////////////////////////
    
    class Rectangle
    {
    public:
    	Rectangle(const int w, const int h) 
    		: width(w), height(h)
    	{};
    
    	~Rectangle() {};
    	int& operator() (const size_t i);
    
    public:
    	int width;
    	int height;
    };
    
    
    //////////////////////////////////////////////////////////
    
    int & 
    Rectangle::operator()(const size_t i)
    {
    	if (i == 0)
    		return width;
    	else
    		return height;
    }
    
    
    //////////////////////////////////////////////////////////
    
    std::ostream&
    operator<< (std::ostream& os, const Rectangle& rec)
    {
    	os << rec.width << ", " << rec.height;
    	return  os;
    
    }
    
    //////////////////////////////////////////////////////////
    
    int main()
    {
    	Rectangle a(40, 10);
    	
    	std::cout
    		<< "w = " << a(0) << std::endl		// 输出 40
    		<< "h = " << a(1) << std::endl		// 输出 10
    		;
    	
    
    	return 0;
    }
    

      

  • 相关阅读:
    项目质量管理
    项目成本管理
    项目进度管理
    项目范围管理
    项目整体管理
    项目立项管理
    信息系统项目管理基础
    信息化和信息系统
    linux(3)
    Patorjk
  • 原文地址:https://www.cnblogs.com/alexYuin/p/11965912.html
Copyright © 2011-2022 走看看