【问题标题】:c++ and operator precedencesc++ 和运算符优先级
【发布时间】:2013-07-31 22:57:24
【问题描述】:

我知道这个长字符串可以更容易阅读,但我不希望这样!

我想获得一个像素的颜色,我正在使用 SDL。虽然这与问题不太相关......

http://www.gamedev.net/topic/502040-sdl-get-pixel-color/

http://www.libsdl.org/docs/html/sdlsurface.html

表明要获得此颜色值,您可以:

 Uint32 *pixels = (Uint32 *)surface->pixels;
  return pixels[ number ];

好吧,我没有那样的东西,我还想尝试掌握整个运算符优先级的东西..

我已经尝试了一些,但我无法让它与最后一个 [] 运算符一起使用。

所以...我明白了:

vector<Class*>* pointer_To_A_Vector_With_Pointers;

Class.h: 
vector<Class2*>* get_Another_Vector(); 

Class2.h
SDL_Surface* sdlSurface; 

SDL_Surface.h
has the pixels-array 




Uint32 value =  *(Uint32*) (* pointer_To_A_Vector_With__Pointers[i]->get_Another_Vector()  )[i2]->sdlSurface->pixels;

应该相当于这样说:

   Uint32 *pixels = (Uint32 *)surface->pixels;

它可以工作,但只检索像素数组的第一种颜色。但我想实现这一点(行尾的 [number] ):

Uint32 value =  *(Uint32*) (* pointer_To_A_Vector_With__Pointers[i]->get_Another_Vector()  )[i2]->sdlSurface->pixels[ number ];

换句话说,我希望包含最后一个 operator[] sdlSurface-&gt;pixels[numbers]

【问题讨论】:

标签: c++ pointers operator-keyword operator-precedence


【解决方案1】:

[] 的优先级高于 *,所以:

 *pointer_To_A_Vector_With__Pointers[i]->get_Another_Vector() 

应该是:

 (*pointer_To_A_Vector_With__Pointers)[i]->get_Another_Vector() 

正如你的变量名所暗示的那样。

【讨论】:

  • @jrok 你的意思是第一个表达式给出编译错误吗?
  • 不,我的意思是我不知道它是有效的。
  • @jrok :) 好吧,这是猜测的答案,让我们 OP 尝试其他我将删除我的答案。
  • 嗨,我没有让它工作,但在我继续之前:我有点困惑,让我换个说法:我当前的版本允许 [i] 工作,问题是最后一个运算符 [number] 显示在我的开篇文章的最后一行代码中。 (我还编辑了我的问题以获得更好的解释)
  • @jrok:当然,A[B]-&gt;C 等价于(*A[B]).C,只要A[B] 是一个指针。
猜你喜欢
  • 2021-03-29
  • 1970-01-01
  • 2013-02-24
  • 2017-06-02
  • 1970-01-01
  • 2014-11-09
  • 2021-03-16
  • 2011-06-21
相关资源
最近更新 更多