Flow Flow - 1 year ago 73
C++ Question

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
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!

is part of the SFML library: https://github.com/SFML/SFML/blob/master/src/SFML/Graphics/VertexArray.cpp

Answer Source

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?

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download