Page 2 of 2 FirstFirst 12
Results 11 to 16 of 16

Thread: Portin from Python to C++

  1. #11
    Join Date
    Apr 2012
    Beans
    39

    Re: Portin from Python to C++

    Quote Originally Posted by lykwydchykyn View Post
    Right now the biggest thing that sends me for a loop are pointers. I understand what they are, I just don't always understand why they get used when they do. I read C++ code (for example, the code snippets on this page: http://doc.qt.digia.com/qt/webkit-fancybrowser.html) and it's completely non-obvious to me why sometimes variables are declared as pointers and sometimes not.
    Yeah I dunno if I understand all the reasons for using pointers. One reason is because it's quicker and more efficient to create copies of a pointer than to create copies of an entire class.

    Rule of thumb though, don't use pointers unless you need to.

  2. #12
    Join Date
    Feb 2011
    Location
    Somewhere...
    Beans
    1,554
    Distro
    Ubuntu 14.10 Utopic Unicorn

    Re: Portin from Python to C++

    I'm still a learner so I'm also interested in what pointers are primarily used for.

    But I do know one use of pointers in C++ is taking advantage of inheritance and polymorphism. Those 2 features are useful in some cases (I took advantage of them in my Uno playing program by declaring the classes Human and AI as inheriting from Players, and have the Players morph into Human or AI)
    Also, Qt programs and books usually encourage you to use pointers and "new" them instead of declaring the objects directly. Still dunno why

  3. #13
    Join Date
    Jun 2008
    Location
    Tennessee
    Beans
    3,421

    Re: Portin from Python to C++

    I wanted to get back to this conversation with an example of what confuses me about pointer use.

    See the code examples on this page:

    http://zetcode.com/gui/qt4/layoutmanagement/

    Notice that in verticalbox.cpp all the widgets are created as pointers using "new".

    But in main.cpp, "window" is just created directly.

    Why is it different?

  4. #14
    Join Date
    Apr 2005
    Location
    Hampshire, UK
    Beans
    1,274

    Re: Portin from Python to C++

    Quote Originally Posted by lykwydchykyn View Post
    I wanted to get back to this conversation with an example of what confuses me about pointer use.

    See the code examples on this page:

    http://zetcode.com/gui/qt4/layoutmanagement/

    Notice that in verticalbox.cpp all the widgets are created as pointers using "new".

    But in main.cpp, "window" is just created directly.

    Why is it different?
    window isn't given a parent, so Qt doesn't delete it for you - therefore, it's safe to handle destruction yourself. May as well just allocate it on the stack

  5. #15
    Join Date
    Jun 2008
    Location
    Tennessee
    Beans
    3,421

    Re: Portin from Python to C++

    Quote Originally Posted by GeneralZod View Post
    window isn't given a parent, so Qt doesn't delete it for you - therefore, it's safe to handle destruction yourself. May as well just allocate it on the stack
    Would it be safe to generalize that I should only create qobjects/qwidgets on the stack if:

    - They only need to exist temporarily during a function call (e.g. dialog box)
    - I'm in main(), and thus the program will be done when the function returns.

    Fair enough?

  6. #16
    Join Date
    Apr 2005
    Location
    Hampshire, UK
    Beans
    1,274

    Re: Portin from Python to C++

    Quote Originally Posted by lykwydchykyn View Post
    Would it be safe to generalize that I should only create qobjects/qwidgets on the stack if:

    - They only need to exist temporarily during a function call (e.g. dialog box)
    - I'm in main(), and thus the program will be done when the function returns.

    Fair enough?
    Add "and they aren't given a parent", and that sounds like a good rule - I can't think of any exceptions offhand.

Page 2 of 2 FirstFirst 12

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •