QtConcurrent Sequence that is a list of pointers rather than objects themselves
-
Suppose I have some class
class foo{ private: const int m_bar; int m_baz; public: foo(const int bar) : m_bar {bar} {} void function(){/*...does stuff, modifies m_baz, for instance...*/} int baz() const {return m_baz;} }
for const-correctness, m_bar should be const within this class. Because it is const, it is not an assignable class (I can't define an assignment operator for it). Since it is not an assignable class, I cannot create QList<foo>. I can create QList<foo*> just fine.
Suppose I want to do foo::function() on a lot of foo objects at once, using QtConcurrent::map. Naively, I could do
QList<foo> fooList; auto res = QtConcurrent::map(fooList, &foo::function);
but obviously, QList<foo> isn't allowed. But I can't use QList<foo*> either, because the type there is a pointer to foo, which QtConcurrent doesn't recognize for its sequence.
Not sure how to proceed.
(ed: also not sure why it isn't formatting code properly. I'm following the instructions from the linked markup page)
[edit: corrected coding tags: 3 ` before and after SGaist]
-
The function you pass to
map()
takes as the first parameter a reference to object from the list, so you can use a lambda like this:auto res = QtConcurrent::map(fooList, [](foo* f){ f->function(); });
Note that I used a pointer to foo instead of reference to pointer to foo because there's no benefit for a pointer type anyway and it's one less character to type :)
As for the formatting - don't use html tags directly. They are generated for you and that's what the info describes:
To produce a code block in Markdown, simply indent every line of the block by at least 4 spaces or 1 tab
Another way is to wrap your code in special marking:
```cpp
and```
-
The function you pass to
map()
takes as the first parameter a reference to object from the list, so you can use a lambda like this:auto res = QtConcurrent::map(fooList, [](foo* f){ f->function(); });
Note that I used a pointer to foo instead of reference to pointer to foo because there's no benefit for a pointer type anyway and it's one less character to type :)
As for the formatting - don't use html tags directly. They are generated for you and that's what the info describes:
To produce a code block in Markdown, simply indent every line of the block by at least 4 spaces or 1 tab
Another way is to wrap your code in special marking:
```cpp
and```
@Chris-Kawa Awesome thanks, I'll give this a try.
-
The new forum is still evolving. There's gonna be a button to do that easily soon. For now edit the title of your first post and start it with
[Solved]
. To edit a post click on the little wrench icon (on the right) and it's the first button there.