Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QtConcurrent Sequence that is a list of pointers rather than objects themselves

QtConcurrent Sequence that is a list of pointers rather than objects themselves

Scheduled Pinned Locked Moved Solved General and Desktop
qtconcurrent
5 Posts 2 Posters 1.6k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • shaveraS Offline
    shaveraS Offline
    shavera
    wrote on last edited by A Former User
    #1

    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]

    1 Reply Last reply
    0
    • Chris KawaC Offline
      Chris KawaC Offline
      Chris Kawa
      Lifetime Qt Champion
      wrote on last edited by Chris Kawa
      #2

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

      shaveraS 1 Reply Last reply
      0
      • Chris KawaC Chris Kawa

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

        shaveraS Offline
        shaveraS Offline
        shavera
        wrote on last edited by
        #3

        @Chris-Kawa Awesome thanks, I'll give this a try.

        1 Reply Last reply
        0
        • shaveraS Offline
          shaveraS Offline
          shavera
          wrote on last edited by
          #4

          sorry, can't see how to edit replies. That worked great. I haven't yet done much with lambda functions yet, so it didn't occur to me. Also I hope adding the "solved" tag is how you mark a post solved...

          1 Reply Last reply
          0
          • Chris KawaC Offline
            Chris KawaC Offline
            Chris Kawa
            Lifetime Qt Champion
            wrote on last edited by Chris Kawa
            #5

            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.

            1 Reply Last reply
            0

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved