Results 1 to 6 of 6

Thread: What default constructor?

  1. #1
    Join Date
    Jan 2006
    Posts
    70
    Thanks
    13
    Thanked 5 Times in 5 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default What default constructor?

    imagine the following sudo like code

    class A
    {
    A();
    A(int, int);
    };

    main
    {
    A varname; // no problem
    A varname(1, 2); // no problem

    A varname(); // problem !!!
    }

    Why can't you use the so called optional parenthesis in this example?

    My first thought was the compiler sees this as invalid chars for the variable name, but this compiles.

    So i added a line after the problem statement using the varname and then it won't compile.

    This leaves me to beleive that the compiler just ignores the problem statement if you don't acually use it. By the way this is the same for g++ as well as msvc compilers.

    So, is there a good reason why this won't just call the default constructor?

    Just curious ...

  2. #2
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    85
    Thanks
    1
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: What default constructor?

    Qt Code:
    1. A varname();
    To copy to clipboard, switch view to plain text mode 
    Is a declaration of a function which has no parameters, returns "A" and is called varname
    Everything which looks like a function IS A function

  3. #3
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: What default constructor?

    "A varname();" is a declaration of a varname function returning A.

    Edit: Great, someone was a bit faster

  4. #4
    Join Date
    Jan 2006
    Posts
    70
    Thanks
    13
    Thanked 5 Times in 5 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: What default constructor?

    So why is it that:

    A varname(1,2);

    is not a declaration of a function, but instead calls a function constructor and assigns the new instance to a variable called varname.

  5. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: What default constructor?

    Quote Originally Posted by bitChanger
    is not a declaration of a function, but instead calls a function constructor and assigns the new instance to a variable called varname.
    Because you can't specify values in declarations this way.

    This would be a declaration:
    Qt Code:
    1. A function1( int, int );
    2. A function2( int a, int b );
    3. A function3( int a = 1, int b = 2 );
    To copy to clipboard, switch view to plain text mode 

  6. The following user says thank you to jacek for this useful post:

    bitChanger (11th April 2006)

  7. #6
    Join Date
    Jan 2006
    Posts
    70
    Thanks
    13
    Thanked 5 Times in 5 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: What default constructor?

    Got it. Thanks!

Similar Threads

  1. QTreeView default drag action
    By onamatic in forum Qt Programming
    Replies: 2
    Last Post: 1st March 2010, 08:37
  2. Q_INTERFACES with classes having a copy constructor
    By Mike in forum Qt Programming
    Replies: 0
    Last Post: 31st October 2008, 13:40
  3. Getting default "system" palette[solved]
    By maverick_pol in forum Qt Programming
    Replies: 0
    Last Post: 2nd April 2008, 18:34
  4. default parameters in constructor class
    By mickey in forum General Programming
    Replies: 4
    Last Post: 23rd February 2008, 19:44
  5. Q3Frame : no appropraite default constructor available
    By Project25 in forum Qt Programming
    Replies: 1
    Last Post: 28th March 2007, 00:23

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.