#Dereferencing a pointer @ index (syntax help)

7 messages · Page 1 of 1 (latest)

azure blade
#

Is there a prettier way to dereference a iterable pointer when being indexed with [i]? Sorry, I'm struggling to figure out how to word this better.
When grabbing a method (member function?) from the pointer I can simply dereference with the pointer operator ->. Ex.

if ( myPtr[i]->isRunning() == true ) ...

But if I'm not calling a method from the pointer I have to dereference it with parentheses and apostrophes like this (* )[i] which messes with my dyslexia.

if ( (*myPtr)[i] == nullptr ) ...

Does anybody know a way around this awful syntax?

random kestrelBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question run !howto ask.

abstract monolith
#

i dont think you can

abstract monolith
#

but sidenote,

if (myPtr[i]->isRunning()) //returns bool
if (!(*myPtr)[i]) //A null pointer is converted to false implicitly```
upbeat inlet
#

the two code snippets above are doing the dereference at different times. are you sure they are dereferencing the same pointer? the first example gets the i'th index of myPtr and then dereferences it. the second example dereferences myPtr and then gets the i'th index. if you want to consistently do as you do in the first example, then you can use an if init:

if (auto p = myPtr[i]; *p == nullptr) ...
azure blade
#

The code snippets above were examples I made up for the sake of discussion, looks like I made a mistake as Nora points out.
I'll look into if init's tomorrow to see if they visually look better to me in my IDE.

Thank you both very much for your input!

#

!solved