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. Why do Qt use pointers instead of simple variable objects?
Forum Updated to NodeBB v4.3 + New Features

Why do Qt use pointers instead of simple variable objects?

Scheduled Pinned Locked Moved General and Desktop
4 Posts 3 Posters 5.6k Views 1 Watching
  • 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.
  • G Offline
    G Offline
    Ground
    wrote on 21 May 2013, 19:40 last edited by
    #1

    I'm a newbie on Qt, and sometimes i hesitate choosing between using pointers or simple variable objects.
    But as i see all the examples in Qt documentation, there are almost always pointers, so i use pointers, but don't know why really i use pointers? Is there an explanation, do i missed something, somewhere ? I know the general meaning of a pointer in c++ but don't see the link with the use in Qt...? Maybe, i know my lessons good but my practical vision is quite bad !
    Thks in advance :)

    1 Reply Last reply
    0
    • J Offline
      J Offline
      JohanSolo
      wrote on 21 May 2013, 19:43 last edited by
      #2

      Pointers are used because the copy constructor and affectation operator are by designed not implemented in the QObject class.

      `They did not know it was impossible, so they did it.'
      -- Mark Twain

      1 Reply Last reply
      0
      • M Offline
        M Offline
        MuldeR
        wrote on 21 May 2013, 22:58 last edited by
        #3

        If you create Qt objects yourself, it's up to your own choice whether you create the object as an "auto" object (that's probably what you call "simple variable objects") or whether you create it on the heap (via "new" operator, so you get a pointer to the object). What method is preferable depends on the individual situation. Note that local "auto" objects are created on the stack. On the one hand this may have a smaller overhead as compared to allocating the memory on the heap. On the other hand, you might not want to create "big" objects on the stack. Also "auto" objects get destroyed, as soon as they go out of scope, while objects on the heap persist until you destroy them explicitly. Both has it's pros and cons. Again: It depends on the situation what is preferable!

        Furthermore: Many functions in Qt that take an object as argument, actually take a pointer to an object. That's because passing an object "by value" would actually create a copy of the object (via copy constructor) and pass that copy! Creating a new copy of the object has a lot of overhead. At the same time, passing the object "by reference", i.e. as a pointer, will just pass a 32-Bit memory address, which is a very lightweight operation.

        In addition to that, in many cases you want to pass exactly that object to the function, not some copy of it ;-)

        --

        See also:
        http://qt-project.org/forums/viewthread/9349

        My OpenSource software at: http://muldersoft.com/

        Qt v4.8.6 MSVC 2013, static/shared: http://goo.gl/BXqhrS

        Go visit the coop: http://youtu.be/Jay...

        1 Reply Last reply
        0
        • G Offline
          G Offline
          Ground
          wrote on 22 May 2013, 21:04 last edited by
          #4

          Thks for responses, it's more clear now :)

          1 Reply Last reply
          0

          1/4

          21 May 2013, 19:40

          • Login

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