[英]C++ access class struct member using an array pointer
Sorry if the title doesn't make sense. 抱歉,标题不正确。 I don't know how to word it as I'm fairly new to C++.
我不知道该如何措辞,因为我是C ++的新手。 Basically I have this:
基本上我有这个:
sf::VertexArray *vArray;
If I want to access the position
inside, I would have to do this: 如果要访问内部
position
,则必须执行以下操作:
(*vArray)[0].position = ...;
Is there a way to use the arrow notation instead? 有没有办法使用箭头符号呢? Why can't I do this:
vArray[0]->position = ...;
我为什么不能这样做:
vArray[0]->position = ...;
? ?
Any help would be appreciated! 任何帮助,将不胜感激!
EDIT : sf::VertexArray
is part of the SFML library: https://github.com/SFML/SFML/blob/master/src/SFML/Graphics/VertexArray.cpp 编辑 :
sf::VertexArray
是SFML库的一部分: https : //github.com/SFML/SFML/blob/master/src/SFML/Graphics/VertexArray.cpp
If your original line 如果您的原始行
(*vArray)[0].position = ...;
properly illustrates the semantics of your data structure, then the ->
-based analogue would be 正确地说明了数据结构的语义,那么基于
->
的类似物就是
vArray->operator [](0).position = ...;
assuming sf::VertexArray
is a class type with overloaded operator []
. 假设
sf::VertexArray
是带有重载运算符[]
的类类型。 Obviously this second form is much more convoluted and requires an explicit reference to the operator member function, which is why it is a better idea to use a much more elegant first form. 显然,第二种形式更加复杂,并且需要对操作员成员函数的明确引用,这就是为什么使用更好的第一种形式更好的主意。
Alternatively, you can force a ->
into this expression as 或者,您可以强制
->
进入此表达式,如下所示:
(&(*vArray)[0])->position = ...;
but that does not make much practical sense. 但这没有太多实际意义。
You can even combine the two 您甚至可以将两者结合
(&vArray->operator [](0))->position = ...;
to arrive at something even more obfuscated and pointless. 以获得更加模糊和毫无意义的东西。
Anyway, why do you want to have a ->
in this expression? 无论如何,为什么要在此表达式中包含
->
? What are you trying to achieve? 您想达到什么目的?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.