C++ 语言为包含点操作符和解引用操作符的表达式提供了一个同义词:箭头操作符(->)。点操作符(第 1.5.2 节)用于获取类类型对象的成员:
item1.same_isbn(item2); // run the same_isbn member of item1
如果有一个指向 Sales_item 对象的指针(或迭代器),则在使用点操作符前,需对该指针(或迭代器)进行解引用:
Sales_item *sp = &item1;
(*sp).same_isbn(item2); // run same_isbn on object to which sp points
这里,对 sp 进行解引用以获得指定的 Sales_item 对象。然后使用点操作符调用指定对象的 same_isbn 成员函数。在上述用法中,注意必须用圆括号把解引用括起来,因为解引用的优先级低于点操作符。如果漏掉圆括号,则这段代码的含义就完全不同了:
// run the same_isbn member of sp then dereference the result! *sp.same_isbn(item2); // error: sp has no member named same_isbn
这个表达式企图获得 sp 对象的 same_isbn 成员。等价于:
*(sp.same_isbn(item2)); // equivalent to *sp.same_isbn(item2);
然而,sp是一个没有成员的指针;这段代码无法通过编译。
因为编程时很容易忘记圆括号,而且这类代码又经常使用,所以 C++ 为在点操作符后使用的解引用操作定义了一个同义词:箭头操作符(->)。假设有一个指向类类型对象的指针(或迭代器),下面的表达式相互等价:
(*p).foo; // dereference p to get an object and fetch its member named foo p->foo; // equivalent way to fetch the foo from the object to which p points
具体地,可将 same_isbn 的调用重写为:
sp->same_isbn(item2); // equivalent to (*sp).same_isbn(item2)
本文详细解释了C++语言中箭头操作符的用途和使用方法,包括如何在指针或迭代器后使用解引用操作符,以及箭头操作符如何简化表达式并提高代码可读性。
1235

被折叠的 条评论
为什么被折叠?



