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

  

C++ class 内的 () 重载示例的更多相关文章

  1. C++ class 内的 [] 重载示例。

    #include <iostream> // overloading "operator [] " inside class ///////////////////// ...

  2. C++ class内的 < 和 > 重载,大于号,小于号,重载示例。

    #include <iostream> // overloading "operator = " outside class // < 和 > 是二元操作符 ...

  3. C++ class内的=重载,拷贝赋值函数copy op=,重载示例。必须是class内

    #include <iostream> // overloading "operator = " inside class // = 是一元操作符.不写,编译器会提供 ...

  4. C++ class内的==重载,判断相等,测试等于,重载示例。二元操作符

    #include <iostream> // overloading "operator == " inside class // == 是二元操作符 //////// ...

  5. C++ class外的 >> 重载,输入流,重载示例。不应该定义类内的>>重载

    #include <iostream> // overloading "operator >> " outside class // >> 应该 ...

  6. C++ class内的 ++ 重载,左++,右++,重载示例。

    #include <iostream> // overloading "operator ++ " inside class // ++ 是一元操作符 //////// ...

  7. C++ class外的 << 重载,输出流,重载示例。不应该定义类内的<<重载

    #include <iostream> // overloading "operator << " outside class // << 应该 ...

  8. 一、小程序内嵌Html示例

    小程序内嵌Html 1.下载wxParse:https://github.com/icindy/wxParse 2.下载完成后将插件目录下的wxParse文件夹拷贝到项目目录下 (文件夹明细) 3.全 ...

  9. 计算时间:一个C++运算符重载示例

    Time类是一个用于计算时间的类,其原型如下:程序清单11.1 mytime0.h // mytime0.h -- Time class before operator overloading #if ...

随机推荐

  1. 【原创】flash中DataGrid数据列显示顺序的解决办法(非数据排序)

    今天在用flash做一个简单的地图展示功能,需要把xml绑定到DataGrid,完成后,又仔细看了几遍,发现列的顺序不对,准确的说是不稳定,不固定,于是在网上查了一下,没有相关的内容.于是自己研究了一 ...

  2. Fiddler抓取内容自动保存本地

    Fiddler抓取内容自动保存本地 一.点击FidderScript 二.选择你想要的获得内容的方法 OnBeforeRequest:发送请求后 OnBeforeResponse:获得响应 三.修改j ...

  3. Attach Files to Objects 将文件附加到对象

    In this lesson, you will learn how to attach file collections to business objects. For this purpose, ...

  4. FCC---CSS Flexbox: Add Flex Superpowers to the Tweet Embed

    To the right is the tweet embed that will be used as the practical example. Some of the elements wou ...

  5. angular cli + primeNG

    目录: 1.安装  angular cli 2.创建项目 3.构建路由 4.新建组件 5.组件之间的通信 6.引入primeNG 7.修改primeNG组件样式 8.问题 -------------- ...

  6. MYSQL之事务篇

    目录 事务概述 事务隔离级别 Read Uncommitted Read Committed Repeatable Read Serializable 事务概述 在引入事务之前我们先考虑银行转账的操作 ...

  7. Git仓库分支管理

    #前提条件:#一.使用命令“cd /d/BranchMgr”进入到需要进行分支管理的仓库的目录,“/d/BranchMgr”是仓库路径.如下图: ”Administrator@DESKTOP-VL6G ...

  8. 阿里云ECS服务器部署HADOOP集群(五):Pig 安装

    本篇将在阿里云ECS服务器部署HADOOP集群(一):Hadoop完全分布式集群环境搭建的基础上搭建. 1 环境介绍 一台阿里云ECS服务器:master 操作系统:CentOS 7.3 Hadoop ...

  9. i2c中start和restart的区别【转】

    有的硬件芯片提供了一个个寄存器,供我们很好的操作i2c,但是,在用的时候,我们是不知道他到地是怎么操作的,下边,我就探讨下i2c中的start和restart的区别. start是在scl是高电平的时 ...

  10. postgres centos 创建数据库 创建用户

    一个小的流程关于如何创建数据库和用户,用以加强印象,以及留档备份 一.创建账户 1.登录postgres账户 su postgres 2.进入psql 指令 psql 3.创建用户 create US ...