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. [Solved] Requirement of default constructors and validity of class instances
Forum Updated to NodeBB v4.3 + New Features

[Solved] Requirement of default constructors and validity of class instances

Scheduled Pinned Locked Moved General and Desktop
6 Posts 3 Posters 3.7k 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
    Ghost
    wrote on 15 Aug 2013, 11:54 last edited by
    #1

    I have been in a habit of disabling construction of objects in an empty or invalid/incomplete state. However, if I want to use signal slot mechanism, I am forced to have a default constructor, for which I have to throw my habit out of the window. it means that my objects could be constructed in an invalid state.
    I am wondering how to get around this.

    Also, say, if a method is called on an invalid object, how to handle that situation? I used to throw exceptions in C#. In Qt, I find the usage rather uncommon, or at least awkward, given the issues of handling uncatched exceptions.

    Bhoot

    1 Reply Last reply
    0
    • J Offline
      J Offline
      Jeroentjehome
      wrote on 15 Aug 2013, 15:07 last edited by
      #2

      Hi,
      First of all the exception handling is discussed in length in the forum. Take a look there and ask it in a separate post.
      On your first issue:
      For Signal/Slot to work you do not need a default constructor! Only a constructor will be enough if you subclass QObject and use the Q_OBJECT macro in your class definition.
      You may have eg:
      @
      explicit void MyClass(quint32 MyId, QObject * parent = 0);
      @
      as constructor and make the default constructor private so an empty class may not be constructed.
      I do this all the time, because like you said, it's a bad thing to have a "uninitialized" class waiting for a crash.

      Greetz, Jeroen

      1 Reply Last reply
      0
      • G Offline
        G Offline
        Ghost
        wrote on 15 Aug 2013, 15:13 last edited by
        #3

        I had to face the issue of the default constructor when I had wanted to register a class in qRegisterMetaType.

        And, regarding a custom constructor, how should an invalid argument - say, a nullptr or an essential, but empty QString? If I create an object from such arguments, it will still be invalid (specially a nullptr gives me creeps).
        Forgive me if I am being silly, but I am baffled as to how to handle such situations in Qt.

        [quote author="Jeroentje@home" date="1376579245"]Hi,
        First of all the exception handling is discussed in length in the forum. Take a look there and ask it in a separate post.
        On your first issue:
        For Signal/Slot to work you do not need a default constructor! Only a constructor will be enough if you subclass QObject and use the Q_OBJECT macro in your class definition.
        You may have eg:
        @
        explicit void MyClass(quint32 MyId, QObject * parent = 0);
        @
        as constructor and make the default constructor private so an empty class may not be constructed.
        I do this all the time, because like you said, it's a bad thing to have a "uninitialized" class waiting for a crash.[/quote]

        Bhoot

        1 Reply Last reply
        0
        • S Offline
          S Offline
          SGaist
          Lifetime Qt Champion
          wrote on 15 Aug 2013, 20:23 last edited by
          #4

          Hi,

          If you take a look at i.e. QDateTime and similar classes or QModelIndex, you'll see that their default construction state is invalid and they provide the isValid() method to check that.

          It's pretty common through Qt to handle that case like that:

          @
          {
          if (!myVariable.isValid())
          return;

          // standard code path is the index is valid
          }
          @

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0
          • G Offline
            G Offline
            Ghost
            wrote on 15 Aug 2013, 21:27 last edited by
            #5

            OK. That looks good. It also seems that documenting the API is as important as safeguarding the code.

            [quote author="SGaist" date="1376598226"]Hi,

            If you take a look at i.e. QDateTime and similar classes or QModelIndex, you'll see that their default construction state is invalid and they provide the isValid() method to check that.

            It's pretty common through Qt to handle that case like that:

            @
            {
            if (!myVariable.isValid())
            return;

            // standard code path is the index is valid
            }
            @[/quote]

            Bhoot

            1 Reply Last reply
            0
            • S Offline
              S Offline
              SGaist
              Lifetime Qt Champion
              wrote on 15 Aug 2013, 21:31 last edited by
              #6

              Indeed, without good documentation, an API gets hard to use and looses interest.

              Just have a look at Qt's documentation, pretty much complete and clean. One of the best you can find.

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              0

              1/6

              15 Aug 2013, 11:54

              • Login

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