指向class的指针使用方法实例
// pointer to classes example
#include <iostream>
using namespace std; class Rectangle {
int width, height;
public:
Rectangle(int x, int y) : width(x), height(y) {}
int area(void) { return width * height; }
}; int main() {
Rectangle obj (3, 4);
Rectangle * foo, * bar, * baz;
foo = &obj;
bar = new Rectangle (5, 6);
baz = new Rectangle[2] { {2,5}, {3,6} };
cout << "obj's area: " << obj.area() << '\n';
cout << "*foo's area: " << foo->area() << '\n';
cout << "*bar's area: " << bar->area() << '\n';
cout << "baz[0]'s area:" << baz[0].area() << '\n';
cout << "baz[1]'s area:" << baz[1].area() << '\n';
delete bar;
delete[] baz;
return 0;
}
运行结果:
obj's area: 12
*foo's area: 12
*bar's area: 30
baz[0]'s area:10
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的指针使用方法实例的更多相关文章
- 【转】const int *p和int * const p的区别(常量指针与指向常量的指针)
[转]作者:xwdreamer 出处:http://www.cnblogs.com/xwdreamer 对于指针和常量,有以下三种形式都是正确的: const char * myPtr = &am ...
- C语言基础语法之指向函数的指针
指针是C语言的精髓,对于初学者来讲,指针是C语言语法学习中比较难的知识点,而这里面指向函数的指针更是不太容易理解. 下面给大家讲下怎样学习理解C语言中指向函数的指针及编程方法和使用例子. 注意:这是一 ...
- [原创]java WEB学习笔记102:Spring学习---Spring Bean配置:bean配置方式(工厂方法(静态工厂方法 & 实例工厂方法)、FactoryBean) 全类名
本博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用 内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系. 本人互联网技术爱 ...
- Java中==、equals、hashcode的区别与重写equals以及hashcode方法实例(转)
Java中==.equals.hashcode的区别与重写equals以及hashcode方法实例 原文地址:http://www.cnblogs.com/luankun0214/p/4421770 ...
- 指向函数的指针与iOS-Block相关知识
指向函数的指针与iOS-Block相关知识 一. 函数指针的定义和调用: 关于函数指针的知识详细可参考:http://www.cnblogs.com/mjios/archive/2013/03/19/ ...
- 【转】Java中==、equals、hashcode的区别与重写equals以及hashcode方法实例
原文地址:http://www.cnblogs.com/luankun0214/p/4421770.html 感谢网友的分享,记录下来只为学习. 1.重写equals方法实例 部分代码参考http ...
- const 指针与指向const的指针
最近在复习C++,指针这块真的是重难点,很久了也没有去理会,今晚好好总结一下const指针,好久没有写过博客了,记录一下~ const指针的定义: const指针是指针变量的值一经初始化,就不可以改变 ...
- 《C++ Primer》之指向函数的指针
函数指针是指指向函数而非指向对象的指针.像其他指针一样,函数指针也指向某个特定的类型.函数类型由其返回类型以及形参表确定,而与函数名无关: // pf points to function retur ...
- C++指向常量的指针和常指针
C++指向常量的指针和常指针 指向常量的指针 通常情况下,可以通过指针去修改指针指向的内容.但是在某些情况下,只希望通过指针去访问指针指向的内容,不想修改.比如只想通过树根结点的指针去遍历输出树中所有 ...
随机推荐
- Linux下mysql定时备份及恢复
备份 1.数据库定时备份工作脚本:(日期时间作为名称的压缩文件,解压开是sql脚本) /root/backup/script/backup_mysql.sh 2.备份输出路径: /root/backu ...
- 使用:/usr/bin/phpize 报错
使用:/usr/bin/phpize 出现下面错误提示 Can't find PHP headers in /usr/include/php The php-devel package is requ ...
- linux下使用shell脚本批处理命令
1.新建脚本touch first.sh 2.写入命令vi first.sh #!/bin/bash #publish service and api echo "copy file&quo ...
- spring数组注入
数组注入 public class MyCollection { private String[]array; private List<String>list; ...
- 邓俊辉数据结构学习-8-2-B树
B树 概述 动机: B树实现高速I/O 640K如何"满足"任何实际需求了-- 源自比尔·盖茨的一个笑话 前提知识 高速缓存 为什么高速缓存有效? 不同容量的存储器,访问速度差异悬 ...
- KBEngine warring项目源码阅读(三) 实体文件与Account处理
上一篇开始,我们就提到了一个概念,并且进行了初步的运用,这个概念就是实体. KBE中的实体是一个很重要的概念,可以说,有了实体就有了一切. 我们首先接着上一章的内容,来看Account.def对应的实 ...
- Android Asynchronous Http Client
Features Make asynchronous HTTP requests, handle responses in anonymous callbacks HTTP requests happ ...
- 大数据kafka视频教程 学习记录【B站尚硅谷 】
视频地址: https://www.bilibili.com/video/av35354301/?p=1 2019/03/06 21:59 消息队列的内部实现: Kafka基础: ...
- Spring Cloud 服务发现和消费
服务的发现和消费 有了服务中心和服务提供者,下面我们来实现一个服务的消费者: 服务消费者主要完成两个任务——服务的发现和服务的消费,服务发现的任务是由Eureka客户端完成,而服务消费的任务是由Rib ...
- jquery初始
今天我们来学习Jquery的一些基本知识,jquery相对来说还是比较重要的,所以还是要好好学习的. 首先要了解什么是jQuery? l类似于python里面的模块,可以看成是一种库或者插件. 在学习 ...