Thread: Newbie Syntax Question (Start of subroutine)

  1. #1
    Registered User TangoOversway's Avatar
    Join Date
    Mar 2013
    Posts
    20

    Newbie Syntax Question (Start of subroutine)

    I'm sure I should know this, but I can't remember and can't find the answer. I'm well used to Java and Perl, which, of course, have quite a different syntax than C++. I'm embarrassed that I have to ask this.

    I'm reading a tutorial on using QT from C++ (here: Qt4 tutorial), and I see this in almost every example. Here's some code:

    Code:
    Absolute::Absolute(QWidget *parent)    : QWidget(parent)
    {
       QTextEdit *edit = new QTextEdit(this);
       edit->setGeometry(5, 5, 200, 150);
    }
    I know about classes and the double colon, but after that, what is the single colon and the text after that? (": QWidget(parent)")

    (I'm not asking about QT at all - just the notation and the purpose.)

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    I assume you understand what you have there is a definition (some people call it "implementation", but that's the wrong word) of a constructor for a class named Absolute.

    The bit after the single colon ("QWidget(parent)" is called an initialiser list (or initializer to Amer-icans). From the argument list of the constructor, we know that QWidget is the name of a user-defined type. The fact that a type name (QWidget) is mentioned directly in an initialiser list says that it is a base class of Absolute. The "QTWidget(parent)" means that the value of parent supplied as an argument to the constructor is used to initialise the base class.

    So, if we have
    Code:
        QWidget *parent_widget = some_function_that_creates_a_widget();
    
        Absolute ab(parent_widget);
    the constructor you described will be invoked. The QWidget base class of Absolute will be initialised (i.e. its constructor invoked) using the pointer parent_widget. Then the body of the constructor (starting at line 2) will be executed. It happens in that order because the standard says so .....


    Initialisers lists aren't only about initialising base classes though. The can (if a class has them) be used to initialise members of that class, as well as any bases.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  3. #3
    Registered User TangoOversway's Avatar
    Join Date
    Mar 2013
    Posts
    20
    Okay, so the single colon is similar to "extends" in Java? (I know I'm leaving out a lot of details - but is that the general quick summary?)

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by TangoOversway
    so the single colon is similar to "extends" in Java?
    No, not in this context. This is a constructor definition, so as grumpy explained the single colon indicates that the follwing is the initializer list to initialise the base class sub-object(s) and member variables of the object.

    In the context of a class definition, then yes, a single colon (optionally followed by an access specifier then) followed by a class name is similiar to "extends" in Java.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    You realize that you can create an object without using new, right?
    So instead of

    QTextEdit *edit = new QTextEdit(this);
    edit->setGeometry(5, 5, 200, 150);

    You can do

    QTextEdit edit(this);
    edit.setGeometry(5, 5, 200, 150);

    You also realize that if you use new, then you must use delete to free it later, right (or use a smart pointer)?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Registered User
    Join Date
    Aug 2010
    Location
    Poland
    Posts
    733
    Quote Originally Posted by Elysia View Post
    You also realize that if you use new, then you must use delete to free it later, right (or use a smart pointer)?
    You don't realize that in QT windows destroy their children automatically, right?

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by kmdv View Post
    You don't realize that in QT windows destroy their children automatically, right?
    Of course I don't, unless the interface uses a smart pointer of some sort. How am I supposed to know?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #8
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    Quote Originally Posted by Elysia View Post
    Of course I don't, unless the interface uses a smart pointer of some sort. How am I supposed to know?
    it's a fairly common trait of GUI frameworks, including GTK+, wxWidgets, and the one we have here: Qt. I suspect that you're not especially familiar with Qt, and as such, you probably aren't aware of its behavior with respect to resource management. Qt doesn't play well with stack objects, so new is more or less required.

  9. #9
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    O_o

    (Elaborating on the style of ownership seen in such libraries.)

    It isn't really using a smart pointer, but the ownership of the object is given to the parent in exactly the same way you would give ownership of an object to a smart pointer.

    The practical upshot is that destroying a parent destroys children in a hierarchical fashion so you, as the client, don't need to "unhook" layers of ownership.

    Soma

    Code:
    class Base
    {
        // ...
        virtual Base(Base * fParent = 0)
        {
            // fParent->registerChild(this)
        }
        // ...
        virtual ~Base()
        {
            // for each child in Children
            // delete child
        }
        // ...
        void registerChild(Base * fChild)
        {
            // add Child to Children list
        }
        // ...
        List<Base *> mChildren;
        // ...
    };

  10. #10
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    I'm not saying it's wrong. I'm saying it's not self-documenting.
    A raw pointer could mean anything:
    - Is it because a reference is not possible/practical in this case, and hence we just want a pointer to it (and not store it for forward reference)?
    - Is it because we must store a copy for future reference?
    -- If so, are we (the library) responsible for deleting the pointer?
    -- Or is it the programmer who created it who is responsible?

    What makes matters worse is that this is often not documented in the API reference. Sometimes it tends to be documented in "how to use" the library, though.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  11. #11
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    Quote Originally Posted by Elysia View Post
    I'm not saying it's wrong. I'm saying it's not self-documenting.

    What makes matters worse is that this is often not documented in the API reference. Sometimes it tends to be documented in "how to use" the library, though.
    top page -> QT Object model -> object hierarchy

    which reads as follows in the first paragraph:
    QObjects organize themselves in object trees. When you create a QObject with another object as parent, it's added to the parent's children() list, and is deleted when the parent is. It turns out that this approach fits the needs of GUI objects very well. For example, a QShortcut (keyboard shortcut) is a child of the relevant window, so when the user closes that window, the shorcut is deleted too.
    Last edited by whiteflags; 03-25-2013 at 03:14 PM. Reason: fixed a link

  12. #12
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    What is your point? I know it documents the behaviour somewhere, but that wasn't the point.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  13. #13
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    Quote Originally Posted by Elysia View Post
    What is your point? I know it documents the behaviour somewhere, but that wasn't the point.
    I put your observation to the test and found information that you didn't have quite easily.

  14. #14
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    There never was any question as to whether the information could be found easily or not.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  15. #15
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    I have no interest in debating what your point really was. At worst, your backpedaling now because you don't know how QT really works.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 12-17-2011, 06:15 PM
  2. newbie question: how to start ubuntu?
    By Yarin in forum Tech Board
    Replies: 11
    Last Post: 08-14-2008, 05:59 PM
  3. Compiler Syntax Error (Newbie Question)
    By NewCProgrammer in forum C Programming
    Replies: 8
    Last Post: 04-22-2008, 07:45 AM
  4. Replies: 3
    Last Post: 10-31-2006, 02:15 AM
  5. newbie wonders how to start c++
    By buraks78 in forum C++ Programming
    Replies: 3
    Last Post: 06-18-2003, 09:54 AM

Tags for this Thread