C++结构体与类指针知识点总结
在结构体或类中,指针访问其成员函数或变量通过"->"运算符或者看代码注释部分,注释部分的操作不推荐:
#include#include usingnamespacestd; structSTRUCT { stringhello; }; intmain() { STRUCT*str=newSTRUCT; str->hello="Hello";//或者可以写成:(*str).hello="Hello" cout< hello< #include#include usingnamespacestd; classCLASS { public: stringhello; }; intmain() { CLASS*str=newCLASS; str->hello="Hello";//同理 cout< hello< 备注:class中的public不可以省,struct中public可以省(属于语法部分,不做解释)
关于类与结构体的指针都是这样操作(无论是哪种数据类型),
注意:一定要给结构体或类指针声明空间,否则输出可能会是乱码或没有输出,本人更建议使用智能指针,免得申请释放空间
以上就是本次介绍的关于C++结构体与类指针全部知识点内容,感谢大家的阅读和对毛票票的支持。