Cpp:"->"和"."的区别
environments:gcc version 8.1.0 (x86_64-posix-seh-rev0, Built by MinGW-W64 project)
class data{
public:
int x;
data(int vx):vx(x){}
void msg(){}
};
// create class data instances;
data d = data(3);
data* cpt = &d;
d.msg();
cpt->msg();
typedef struct col{
collection(int id, float score):cid{id},cscore{score}{}
int cid;
float cscore;
} collection;
// create struct col object
collection c{1,145.3};
cout << c.cid << endl ;
cout << c.cscore <<endl;
collection* spt = &c;
cout << spt -> cid << endl;
cout << spt-> cscore << endl;
Cpp中“.”和“->”说明:
1、“.”是成员运算符,用于调取成员变量和成员函数;符号“.”的左边必须是实例对象(具体对象),举例为绿色字体;
2、“->”是地址运算符,用于引用成员变量和成员函数;符号“->”的左边是实例对象的地址或者类名(结构名),举例为黄色字体;
3、等价形式:d.msg() 和 (*cpt).msg() 等价;c.cid 和 (*spt).cid;
4、“.”和“->”常用于“类和结构”相关操作;
5、结构体初始化说明:传送门
下面是具体代码:
#include<iostream> using namespace std; class Info{ private:
int iage;
int iid; public:
Info(int age, int id):iage(age),iid(id){}
void msg(){
cout << "age = " << this->iage << "\t";
cout << "id = " << this->iid << endl;
} }; typedef struct Data
{
Data(int id, float math):did{id},dmath{math}{}
float dmath;
int did;
} data; int main(){ Info information = Info(, );
Info* cpt;
information.msg();
cpt = &information;
cpt -> msg(); data d{,};
cout <<"d.id = " << d.did << "\t";
cout <<"d.math = " << d.dmath << endl; data* spt;
spt = &d;
cout <<"spt->id = " << spt->did << "\t";
cout <<"spt->math = " << spt->dmath << endl; return ;
} // result:
// age = 30 id = 1
// age = 30 id = 1
// d.id = 1 d.math = 145
// spt->id = 1 spt->math = 145
最新文章
- NYOJ 975
- [LeetCode] Median of Two Sorted Arrays 两个有序数组的中位数
- 基于Swiper 2.7.6实现的带缩略图功能的轮播图
- 将字符串转换成JSON对象
- websocket 实现聊天功能
- 20161127-adt bundle
- 【使用 DOM】使用 Window 对象
- Inno setup 简单打包教程
- onNewIntent调用时机
- WPF - 使用WPF创建图表
- 安全系列之二:OAuth2.0 开放授权协议
- ABP之多租户
- node多人聊天室搭建
- 左侧 随着页面滚动固定 fixed. scroll .scrollTop
- SpringBoot标签之@ConfigurationProperties、@PropertySource注解的使用
- [Redis]Redis高级特性的配置及使用
- git 使用ssh密钥
- Centos升级Python 2.7并安装pip、ipython
- 装B必备之 快捷键配置
- 帝国CMS“建立目录不成功!请检查目录权限”的解决办法
热门文章
- 解析基于keras深度学习框架下yolov3的算法
- net.sf.json.JSONObject maven下载到了但是java后台一直用不了问题
- (三)微信小程序配置
- netty权威指南学习笔记七——编解码技术之GoogleProtobuf
- Vulkan SDK 之 Shaders
- scala安装教程及简单配置
- 八十七、SAP中ALV事件之一,事件的声明
- ZOJ 1409 communication system 双变量型的DP
- 吴裕雄--天生自然C++语言学习笔记:C++ 多态
- springboot+thymeleaf项目中使用th:replace访问templates子目录下的模板,会报错找不到模板路径