C++什么是" - >"?

Ais*_*tis -1 c++ programming-languages

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

我只是有一个非常基本的问题.无法在谷歌上搜索...我不知道 - >是什么以及它在c ++中做了什么,例如:

render = Surface->Logic->Scale;
Run Code Online (Sandbox Code Playgroud)

iam*_*ind 7

在C++中->有2个上下文:

  1. 关于指针
  2. 关于对象

它取消引用指针,因此基本上它会将变量存储在某个偏移量的内存位置.从而,

Surface->Logic->Scale;
Run Code Online (Sandbox Code Playgroud)

相当于,

(*Surface).Logic->Scale;  // same applies for 'Logic' too
Run Code Online (Sandbox Code Playgroud)

对于object,你可以通过重载它来模拟这个操作符,

struct B { void foo(); };
struct A {
  B *p;
  B* operator -> () { return p; }
};
Run Code Online (Sandbox Code Playgroud)

用法是,

A obj;
obj->foo();
Run Code Online (Sandbox Code Playgroud)