#include <iostream>

// overloading "operator [] " inside 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 () " outside 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. go语言设计模式之Concurrency future

    future.go package future type SuccessFunc func(string) type FailFunc func(error) type ExecuteStringF ...

  2. 一、itk在VS2019上面的安装 和例子(HelloWorld)运行

    一.Itk简介 vtk是专门用于医疗图像处理的函数库,类似opencv. 这篇博客主要是讲解安装vtk之后的例子的运行,即如何构建自己的第一个ITK例子 二.Itk安装 Itk安装参考这篇博客: ht ...

  3. 如何解决android 通知栏不显示的问题

    android 8.0 以后的版本,在创建通知栏的时候,加了一个channelId的东西.要想在上述版本中显示通知,总共分两步 1.创建Channel if (Build.VERSION.SDK_IN ...

  4. JPA的一些问题

    Error creating bean with name 'mainController': Unsatisfied dependency expressed through field 'test ...

  5. R语言-记号体系

    安装xlsx包 #装之前先装jdk,配置环境变量 install.packages("xlsx") 代表安装成功 必须先加载包然后再使用包library() $提取符号 当一个函数 ...

  6. CF1248F Catowice City

    题目链接 problem 有\(n\)个人,每个人家有一只猫.每个人都认识一些猫(其中肯定包括自己家的猫).选出\(j\)个人和\(k\)只猫\((j,k\ge 1)\).使得\(j+k=n\)且选出 ...

  7. golang数据结构之用循环链表解决约瑟夫环问题

    josephu.go package link import ( "fmt" ) type Kid struct { ID int next *Kid } func AddKid( ...

  8. Kubernetes V1.15 二进制部署集群

    1. 架构篇 1.1 kubernetes 架构说明              1.2 Flannel网络架构图 1.3 Kubernetes工作流程             2. 组件介绍 2.1 ...

  9. Maven的assembly插件在linux启动卡住Starting the localhost.localdomain

    1.今天在测试assembly的时候,在Linux虚拟机,内存配置为512mb,然后开始在Linux上运行assembly的时候就会一直卡住  2.停止运行后,查看了下日志 [root@localho ...

  10. 死磕 java同步系列之CountDownLatch源码解析