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] Constructor difference
Forum Updated to NodeBB v4.3 + New Features

[SOLVED] Constructor difference

Scheduled Pinned Locked Moved General and Desktop
5 Posts 2 Posters 1.4k 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.
  • ? Offline
    ? Offline
    A Former User
    wrote on 6 Dec 2014, 09:56 last edited by
    #1

    Hey, could someone explain what is the difference between
    @
    explicit MainWindow(QWidget *parent = 0)
    : QWidget(parent) { }@

    and
    @explicit MainWindow(QWidget *parent = 0);@

    At the first example, what could I write inside of the { } ?

    1 Reply Last reply
    0
    • C Offline
      C Offline
      Chris Kawa
      Lifetime Qt Champion
      wrote on 6 Dec 2014, 10:25 last edited by
      #2

      I understand you're a c++ novice? Maybe it would be better to grab a book or a tutorial on classes.

      Anyway, both of these together define a constructor.

      You have an error in the first part. there should be no explicit keyword here and no = 0 in the parameter list. These go only into the declaration in the header (the second snippet).

      The first one is a definition. It goes into the .cpp file. It contains the actual code of the function. That's what you put in the {}. I can't tell you what exactly that is because I don't know what you're program is supposed to do ;) It can also initialize member variables (you heve none here) and call the base class constructor (here it's QWidget(parent)).

      The second one is a declaration. You put it in a header and it describes what name and parameters a function has. It can also define default arguments values.

      1 Reply Last reply
      0
      • ? Offline
        ? Offline
        A Former User
        wrote on 6 Dec 2014, 10:35 last edited by
        #3

        Actually, both of these are from header files... When I create class in Qt it uses second example, but I've seen in some codes on the internet where people are using first example...

        1 Reply Last reply
        0
        • C Offline
          C Offline
          Chris Kawa
          Lifetime Qt Champion
          wrote on 6 Dec 2014, 11:25 last edited by
          #4

          Oh, then someone is doing "the wrong thing"™ and is putting his code in the header, which you shouldn't do. That's usually good for examples though as it's shorter so you will find it on forums and such ;)

          Basically you can do either this (which is what you should):
          @
          //class.h
          class Foo {
          public:
          Foo();
          };

          //class.cpp
          Foo::Foo() {}
          @
          Or this:
          @
          //class.h
          class Foo {
          public:
          Foo() {}
          };
          @
          The second one is shorter but will not compile if you happen to include that header in multiple places.

          1 Reply Last reply
          0
          • ? Offline
            ? Offline
            A Former User
            wrote on 6 Dec 2014, 11:27 last edited by
            #5

            I understand now, thank you...

            1 Reply Last reply
            0

            1/5

            6 Dec 2014, 09:56

            • Login

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