可能的重复:
C++ 中的点 (.) 运算符和 -> 有什么区别? https://stackoverflow.com/questions/1238613/what-is-the-difference-between-the-dot-operator-and-in-c
C++ 中箭头运算符 (->) 的同义词是什么? https://stackoverflow.com/questions/221346/what-is-the-arrow-operator-synonym-for-in-c
标题说明了一切。
什么是->在C++中是什么意思?
就是通过一个对象来访问一个对象的成员函数或者成员变量pointer,而不是常规变量或引用。
例如:对于常规变量或引用,您可以使用.
运算符访问成员函数或成员变量。
std::string s = "abc";
std::cout << s.length() << std::endl;
但如果您使用指针,则需要使用->
操作员:
std::string* s = new std::string("abc");
std::cout << s->length() << std::endl;
它还可以被重载以针对特定对象类型执行特定功能。智能指针如shared_ptr
and unique_ptr
以及 STL 容器迭代器,重载此运算符以模仿本机指针语义。
例如:
std::map<int, int>::iterator it = mymap.begin(), end = mymap.end();
for (; it != end; ++it)
std::cout << it->first << std::endl;
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)