Results 1 to 2 of 2

Thread: Question about constructors and inheritance

  1. #1
    Join Date
    Feb 2012
    Posts
    11
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Question about constructors and inheritance

    Hey everyone,

    I have a (rather simple) question about C++ and constructors/inheritance. Usually, when reading Qt tutorials, I see something like this for the definition of the class's constructor:
    Qt Code:
    1. class MyWidget: public QWidget {
    2. Q_OBJECT
    3. public:
    4. explicit MyWidget(QWidget *parent = 0);
    5. }
    6.  
    7. MyWidget::MyWidget(QWidget *parent = 0): QWidget(parent) {
    8. // Code here
    9. }
    To copy to clipboard, switch view to plain text mode 

    Is it any different from doing it like this:
    Qt Code:
    1. class MyWidget: public QWidget {
    2. Q_OBJECT
    3. public:
    4. explicit MyWidget(QWidget *parent = 0);
    5. }
    6.  
    7. MyWidget::MyWidget(QWidget *parent = 0) {
    8. QWidget(parent)
    9. // Code here
    10. }
    To copy to clipboard, switch view to plain text mode 

    My guess is that QWidget(parent) is a call to the superclass's constructor, leaving the MyWidget constructor to initialize only what else it adds on to it. And maybe that's why it's put outside the body of the function. Put aren't they in practice the same?


    Cheers

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Question about constructors and inheritance

    The first example the constructor is using the initialisation list to control the construction of its inherited QWidget component. This is the only way to control the construction of the underlying inherited class(-es).

    The second example is not doing what you think. MyWidget starts off with a default constructed QWidget at the point it enters the constructor body. Your code as written is treated as a declaration of another QWidget called parent by my compiler and generates a warning because the name clashes with the parameter. (Your compiler might make something else of it.) I suspect you intended to call the QWidget constructor directly, i.e. QWidget::QWidget(parent) : you cannot do this and it will generate an error. It would make little sense anyway, since the underlying QWidget is already present and constructed.

    You can initialise member variables in either the initialisation list or by assignment in the constructor body. Have a read Should my constructors use "initialization lists" or "assignment"?

Similar Threads

  1. Multiple inheritance question
    By marcvanriet in forum Newbie
    Replies: 4
    Last Post: 4th October 2010, 10:30
  2. question about inheritance
    By adamatic in forum Qt Programming
    Replies: 1
    Last Post: 16th February 2009, 10:32
  3. Using template in constructors
    By estanisgeyer in forum General Programming
    Replies: 4
    Last Post: 18th November 2008, 18:41
  4. Inheritance Question
    By RY in forum Newbie
    Replies: 2
    Last Post: 3rd October 2008, 08:13
  5. constructors, new, return
    By TheKedge in forum General Programming
    Replies: 1
    Last Post: 29th September 2006, 14:43

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.