指向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++指向常量的指针和常指针 指向常量的指针 通常情况下,可以通过指针去修改指针指向的内容.但是在某些情况下,只希望通过指针去访问指针指向的内容,不想修改.比如只想通过树根结点的指针去遍历输出树中所有 ...
随机推荐
- 来自于51CTO的经典学习资料汇总
移动开发类: 1.2012Android开发热门资料(110个) http://bbs.51cto.com/thread-934023-1.html 2.[绝对给力]Android开发免豆 ...
- Default Bearer, Dedicated Bearer... What exactly is bearer ?
Default Bearer, Dedicated Bearer... What exactly is bearer ? While trying to get a better understa ...
- magento新增商品属性以及将属性加入Flat table
magento的EAV模型非常强大且灵活,但是如果不做优化的话,性能会非常低,因为attributes都存放在附表里,要获取一个entity的attribute,需要表联结一次,如果需要获取多条att ...
- testNG测试基础一
1.TestNG概念 TestNG:Testing Next Generation 下一代测试技术,是一套根据JUnit和Nunit思想构建的利用注释来强化测试功能的测试框架,可用来做单元测试,也可用 ...
- unicode字符和多字节字符的相互转换接口
作者:朱金灿 来源:http://blog.csdn.net/clever101 发现开源代码的可利用资源真多,从sqlite3的源码中抠出了几个字符转换接口,稍微改造下了发现还挺好用的.下面是实现代 ...
- 如何使用VS将项目生成一个安装包?
VS2010项目的部署与安装winform程序,我想进行安装.1.在解决方案中 ——点击右键——添加 2.然后选择 安装和部署 ——安装向导 可以更改名称 3.点击 下一步 4.然后选择上那3个 5. ...
- dac verilog ad5601
首先从官网下载数据手册.DAC有串行有并行,ad5601是串行,(需要好多时钟沿的移位内部转换为并行在输出). 按照手册的时序编写程序, 关注下芯片的波特率范围 看看手册的数据传输那些事有效的数据位 ...
- html常用的小技能
在html中有很多常用小技能,记下来,下次直接看自己的,就不用四处找啦! 1.<li>标签去掉点号:list-style-type:none; 去掉前: 去掉后: 2.<li> ...
- win10蓝牙添加设备无法连接
解决方法: 打开运行窗口,输入services.msc. 找到蓝牙支持服务(或者Bluetooth Support Service),右键,属性,启动类型选择手动,启动服务. 还不行的话,此电脑右键, ...
- 新建snmp模型总结
1.在DeviceType.xml中添加新的模块 2.在modellidx.json中添加路径关联 3.添加定义模型 4.定义model.xml注意: 5.定义collect.xml注意: