现在的位置: 首页 > 编程语言 > 正文

C++结构体与类指针知识点总结

2020年02月14日 编程语言 ⁄ 共 717字 ⁄ 字号 评论关闭

在结构体或类中, 指针访问其成员函数或变量通过 "->" 运算符或者看代码注释部分, 注释部分的操作不推荐:

#include <iostream>#include <cstring>using namespace std;struct STRUCT{ string hello;};int main(){ STRUCT *str=new STRUCT; str->hello="Hello";//或者可以写成: (*str).hello="Hello" cout<<str->hello<<endl;//或者可以写成: cout<<(*str).hello<<endl; delete str; return 0;}

#include <iostream>#include <cstring>using namespace std;class CLASS{public: string hello;};int main(){ CLASS *str=new CLASS; str->hello="Hello";//同理 cout<<str->hello<<endl;//同理 delete str; return 0;}

备注: class中的public不可以省, struct中public可以省 ( 属于语法部分, 不做解释 )

关于类与结构体的指针都是这样操作 (无论是哪种数据类型),

注意: 一定要给结构体或类指针声明空间, 否则输出可能会是乱码或没有输出, 本人更建议使用智能指针, 免得申请释放空间

以上就是本次介绍的关于C++结构体与类指针全部知识点内容,感谢大家的阅读和对我们的支持。

本文标题: C++结构体与类指针知识点总结

以上就上有关C++结构体与类指针知识点总结的相关介绍,要了解更多C++,结构体,类指针内容请登录学步园。

抱歉!评论已关闭.