Download

Confusion around TArray iterators

So I have a TArray of objects I’d like to iterate over and an iterator seems like the best way to do it. Unfortunately the iterator is advanced by callbacks from background processes, so I’ve been trying to implement the iterator as a class member.

I’m mostly just getting confused and running into syntax errors, but basically: static auto types must be initialized, and I’m not sure what to initialize it with. Or something.

So this is what I started with:


struct MyJobStruct    {
    JobType Type;
    FString ObjectName;

};

and…


static TArray <MyJobStruct> JobQueue;


And the docs suggest:


for (auto Job = JobQueue.CreateConstIterator(); Job; ++Job){
    NewJob(Job)

}

So naturally I think I could do:

Header


static auto Job;

C++


MyThing::Job = JobQueue.CreateConstIterator()

Callback


NewJob(++Job)

But obviously I run into initialization problems. If I make Job into a TConstIterator I can no longer access any of the members of my Job struct. How do I do this?

Also:


CurrentJob = JobQueue.CreateConstIterator();

Doesn’t work in this context at all. I’m so lost.

Might be totally silly, but why use .CreateConstIterator() instead of .CreateIterator() in this context? Sounds like you want to be able to change the elements!

I just pulled that straight out of the docs. It was probably a poor choice, yes. I’ve reverted to using an index for now, but I’ll give that a crack.

I don’t know if that’ll address the issue that what CreateConstIterator() says it wants to create and what it actually creates are completely foreign to each other. I’ll have to set it up again to see what that was though.

Hello! How about this:




for (auto& JobElem : JobQueue)
{
    //some code
}


That’s the exact opposite of what I want to do. :slight_smile: