首页 > 编程 > C++ > 正文

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

2020-01-26 13:25:06
字体:
来源:转载
供稿:网友

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

#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++结构体与类指针全部知识点内容,感谢大家的阅读和对武林网的支持。

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表