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. Can somone help explain some fundementals of c++? I keep seeing this stuff over and over and just want that "push" to help me understand.
Forum Updated to NodeBB v4.3 + New Features

Can somone help explain some fundementals of c++? I keep seeing this stuff over and over and just want that "push" to help me understand.

Scheduled Pinned Locked Moved General and Desktop
c++constreferancepointer
4 Posts 4 Posters 1.9k Views 3 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.
  • R Offline
    R Offline
    Rample
    wrote on 11 Jun 2015, 23:53 last edited by Rample 6 Nov 2015, 23:54
    #1

    So I understand what everything does in the code below. However I don't understand the reasoning behind it.

    • Why is "const" used the way it is used? I feel like const is just "peppered" throughout c++ and I never know why its being used.
    • why does the constructor take in a reference to a string, and a pointer to a Widget? (whats the explicit reasoning for using each ?)

    this code is from the fridge magnet example :

    class DragLabel : public QLabel
    {
    public:
    DragLabel(const QString &text, QWidget *parent);
    QString labelText() const;

    private:
    QString m_labelText;
    };

    J 1 Reply Last reply 12 Jun 2015, 04:11
    0
    • A Offline
      A Offline
      alex_malyu
      wrote on 12 Jun 2015, 00:45 last edited by alex_malyu 6 Dec 2015, 00:50
      #2

      I will try:

      DragLabel(const QString &text, QWidget *parent);

      const is used to tell compiler that the value is not going to be changed inside, which allows it to build more efficient code.
      Story starts from the fact that you want to pass existing string to function.

      But if you write

      • DragLabel( QString text, QWidget *parent);
        Compile will have to copy string value which is slow and it is not needed.

      in such cases you pass const references and it means that no copy occurs and from within function actually an address ( reference is an address ) will be used to access the value of existing outside QString object.

      Why QWidget *parent)?

      Cause reference is an address to existing object. Parent may or may not be defined and thus pointer is used.
      Pointer is also an address, but it may point to NULL when reference must always point to valid object.

      (Technically you are able to achieve what is strictly forbidden and get reference to invalid or NULL pointer using language features, but if you find such code in real life you better stay away from such programmer as far as you can)

      Hope this helps, but book would describe this much better.

      1 Reply Last reply
      1
      • R Rample
        11 Jun 2015, 23:53

        So I understand what everything does in the code below. However I don't understand the reasoning behind it.

        • Why is "const" used the way it is used? I feel like const is just "peppered" throughout c++ and I never know why its being used.
        • why does the constructor take in a reference to a string, and a pointer to a Widget? (whats the explicit reasoning for using each ?)

        this code is from the fridge magnet example :

        class DragLabel : public QLabel
        {
        public:
        DragLabel(const QString &text, QWidget *parent);
        QString labelText() const;

        private:
        QString m_labelText;
        };

        J Offline
        J Offline
        JKSH
        Moderators
        wrote on 12 Jun 2015, 04:11 last edited by JKSH 6 Dec 2015, 04:12
        #3

        Hi,

        @Rample said:

        • Why is "const" used the way it is used? I feel like const is just "peppered" throughout c++ and I never know why its being used.

        These articles might help:

        • http://www.cprogramming.com/tutorial/const_correctness.html
        • http://stackoverflow.com/questions/136880/sell-me-on-const-correctness
        • why does the constructor take in a reference to a string, and a pointer to a Widget? (whats the explicit reasoning for using each ?)

        There are 2 categories of classes: Value classes and Identity classes.

        • Value objects are simply data: strings, timestamps, images, and so on. You can copy the data inside value objects without any issues.
        • Identity objects, on the other hand, are not copyable.
          • Take QFile for example -- what should happen if you "copy" a QFile object? Should the file on actual disk also be copied? Or should the actual file be left alone, and just have two QFile objects accessing the same file? Neither option is sensible, so Qt disallows you from copying the QFile.

        In your example, QString is a value class and QWidget is an identity class. (In fact, all QObjects are identity classes.) In the Qt API:

        • Value objects are passed by-const-reference. Think of passing by const-reference as a safer and more efficient form of passing by-value ("copying" without actually creating a copy)
        • Identity classes are passed around by-pointer. They cannot be copied, therefore they cannot be passed by-value.

        See also: Qt Objects: Identity vs Value

        Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

        1 Reply Last reply
        1
        • L Offline
          L Offline
          LuGRU
          wrote on 13 Jun 2015, 23:16 last edited by
          #4

          Why parent ptr?
          Parent child relationship - http://doc.qt.io/qt-5/objecttrees.html

          1 Reply Last reply
          0

          1/4

          11 Jun 2015, 23:53

          • 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