1. // pointer to classes example
  2. #include <iostream>
  3. using namespace std;
  4.  
  5. class Rectangle {
  6. int width, height;
  7. public:
  8. Rectangle(int x, int y) : width(x), height(y) {}
  9. int area(void) { return width * height; }
  10. };
  11.  
  12. int main() {
  13. Rectangle obj (3, 4);
  14. Rectangle * foo, * bar, * baz;
  15. foo = &obj;
  16. bar = new Rectangle (5, 6);
  17. baz = new Rectangle[2] { {2,5}, {3,6} };
  18. cout << "obj's area: " << obj.area() << '\n';
  19. cout << "*foo's area: " << foo->area() << '\n';
  20. cout << "*bar's area: " << bar->area() << '\n';
  21. cout << "baz[0]'s area:" << baz[0].area() << '\n';
  22. cout << "baz[1]'s area:" << baz[1].area() << '\n';
  23. delete bar;
  24. delete[] baz;
  25. return 0;
  26. }

  运行结果:

  1. obj's area: 12
  2. *foo's area: 12
  3. *bar's area: 30
  4. baz[0]'s area:10
  5. baz[1]'s area:18

  指向class的指针。

This example makes use of several operators to operate on objects and pointers (operators *&.->[]). They can be interpreted as:

expression can be read as
*x pointed to by x
&x address of x
x.y member y of object x
x->y member y of object pointed to by x
(*x).y member y of object pointed to by x (equivalent to the previous one)
x[0] first object pointed to by x
x[1] second object pointed to by x
x[n] (n+1)th object pointed to by x

Most of these expressions have been introduced in earlier chapters. Most notably, the chapter about arrays introduced the offset operator ([]) and the chapter about plain data structures introduced the arrow operator (->).

指向class的指针使用方法实例的更多相关文章

  1. 【转】const int *p和int * const p的区别(常量指针与指向常量的指针)

    [转]作者:xwdreamer   出处:http://www.cnblogs.com/xwdreamer 对于指针和常量,有以下三种形式都是正确的: const char * myPtr = &am ...

  2. C语言基础语法之指向函数的指针

    指针是C语言的精髓,对于初学者来讲,指针是C语言语法学习中比较难的知识点,而这里面指向函数的指针更是不太容易理解. 下面给大家讲下怎样学习理解C语言中指向函数的指针及编程方法和使用例子. 注意:这是一 ...

  3. [原创]java WEB学习笔记102:Spring学习---Spring Bean配置:bean配置方式(工厂方法(静态工厂方法 & 实例工厂方法)、FactoryBean) 全类名

    本博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用 内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系. 本人互联网技术爱 ...

  4. Java中==、equals、hashcode的区别与重写equals以及hashcode方法实例(转)

    Java中==.equals.hashcode的区别与重写equals以及hashcode方法实例  原文地址:http://www.cnblogs.com/luankun0214/p/4421770 ...

  5. 指向函数的指针与iOS-Block相关知识

    指向函数的指针与iOS-Block相关知识 一. 函数指针的定义和调用: 关于函数指针的知识详细可参考:http://www.cnblogs.com/mjios/archive/2013/03/19/ ...

  6. 【转】Java中==、equals、hashcode的区别与重写equals以及hashcode方法实例

    原文地址:http://www.cnblogs.com/luankun0214/p/4421770.html 感谢网友的分享,记录下来只为学习. 1.重写equals方法实例   部分代码参考http ...

  7. const 指针与指向const的指针

    最近在复习C++,指针这块真的是重难点,很久了也没有去理会,今晚好好总结一下const指针,好久没有写过博客了,记录一下~ const指针的定义: const指针是指针变量的值一经初始化,就不可以改变 ...

  8. 《C++ Primer》之指向函数的指针

    函数指针是指指向函数而非指向对象的指针.像其他指针一样,函数指针也指向某个特定的类型.函数类型由其返回类型以及形参表确定,而与函数名无关: // pf points to function retur ...

  9. C++指向常量的指针和常指针

    C++指向常量的指针和常指针 指向常量的指针 通常情况下,可以通过指针去修改指针指向的内容.但是在某些情况下,只希望通过指针去访问指针指向的内容,不想修改.比如只想通过树根结点的指针去遍历输出树中所有 ...

随机推荐

  1. 大数据“重磅炸弹”——实时计算框架 Flink

    Flink 学习 项目地址:https://github.com/zhisheng17/flink-learning/ 博客:http://www.54tianzhisheng.cn/tags/Fli ...

  2. img IE下支持最大宽度

    border:0 none; max-width: 560px; height:auto; width:expression(this.width > 600 ? "600px&quo ...

  3. Eucalyptus学习汇总

    Elastic Utility Computing Architecture for Linking Your Programs To Useful Systems (Eucalyptus) 是一种开 ...

  4. 跨平台移动开发phonegap/cordova 3.3全系列教程-目录

    目录(更新完成后会附上源码供参考) 第一章 android平台开发 phonegap/cordova简介 1.开发环境搭建 2.helloworld 3.启动画面 4.结合asp.net/jqmboi ...

  5. 判断一个点是否在多边形区域内--C算法

    /*函数的输入:(1)当前点的坐标p(2)区域顶点数组pt[]:(3)顶点数nCount 输出: 在区域内返回TRUE,否则返回FALSE.  Point类型是一个结构: struct Point { ...

  6. nginx对不存在的文件进行404处理

    location / { try_files $uri $uri/ /?$args 404; } location / { try_files $uri $uri/ /index.html 404; ...

  7. shell实现mysql数据库备份

    #!/bin/bash DB_USER="root" #数据库用户名 DB_PASS="12345678" #数据库密码 BACK_DIR="/bac ...

  8. sublime完美编码主题

    Theme – Soda 使用Ctrl+Shift+P快捷键或者进入菜单:Preferences(首选项) - Package Control(插件控制),调出命令输入框,输入Install Pack ...

  9. TP5.0:的安装与配置

    在网址中输入:localhost/安装TP5的文件夹/public/ 入口文件位置:public/index.php: 最新版本中,新建的文件夹是没有模型和视图的,需要自行添加没有的文件: 添加前: ...

  10. TTTAttributedLabel

    TTTAttributedLabel 库地址 https://github.com/TTTAttributedLabel/TTTAttributedLabel #import "ViewCo ...