syy
syy

Reputation: 807

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++. Basically I have this:

sf::VertexArray *vArray;

If I want to access the position inside, I would have to do this:

(*vArray)[0].position = ...;

Is there a way to use the arrow notation instead? Why can't I do this: 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

Upvotes: 0

Views: 54

Answers (1)

AnT stands with Russia
AnT stands with Russia

Reputation: 320747

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 []. 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?

Upvotes: 3

Related Questions