📜  什么是 -> 在 C++ 代码示例中

📅  最后修改于: 2022-03-11 14:44:54.179000             🧑  作者: Mango

代码示例1
/**
 * @author Charles Salvia (StackOverflow)
 *
 * -> is 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;