什么 - >是什么意思在C ++? [英] What does -> mean in C++?

查看:132
本文介绍了什么 - >是什么意思在C ++?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能的重复项:



什么是C ++中的箭头运算符( - >)同义词?




- > 在C ++中意味着什么?

>感谢:)

解决方案

它是通过指针访问对象的成员函数或成员变量



例如:对于常规变量或引用,使用运算符以访问成员函数或成员变量。

  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 unique_ptr 这样的智能指针以及STL容器迭代器重载这个操作符来模仿本地指针语义。 / p>

例如:

  std :: map< int,int& :: iterator it = mymap.begin(),end = mymap.end(); 
for(; it!= end; ++ it)
std :: cout<第一<< std :: endl;


Possible Duplicates:
What is the difference between the dot (.) operator and -> in C++?
What is the arrow operator (->) synonym for in C++?

The header says it all.

What does -> mean in C++?

Thanks :)

解决方案

It's to access a member function or member variable of an object through a pointer, as opposed to a regular variable or reference.

For example: with a regular variable or reference, you use the . operator to access member functions or member variables.

std::string s = "abc";
std::cout << s.length() << std::endl;

But if you're working with a pointer, you need to use the -> operator:

std::string* s = new std::string("abc");
std::cout << s->length() << std::endl;

It can also be overloaded to perform a specific function for a certain object type. Smart pointers like shared_ptr and unique_ptr, as well as STL container iterators, overload this operator to mimic native pointer semantics.

For example:

std::map<int, int>::iterator it = mymap.begin(), end = mymap.end();
for (; it != end; ++it)
    std::cout << it->first << std::endl;

这篇关于什么 - &gt;是什么意思在C ++?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆