Results 1 to 4 of 4

Thread: C++ Question

  1. #1
    Join Date
    Sep 2012
    Location
    Iran, Tehran
    Posts
    76
    Thanks
    17
    Thanked 13 Times in 13 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Windows

    Question C++ Question

    Below is the implementation of QInputDialog::getText from Qt 4.7
    Qt Code:
    1. QString QInputDialog::getText(QWidget *parent, const QString &title, const QString &label,
    2. QLineEdit::EchoMode mode, const QString &text, bool *ok,
    3. Qt::WindowFlags flags)
    4. {
    5. QInputDialog dialog(parent, flags);
    6. dialog.setWindowTitle(title);
    7. dialog.setLabelText(label);
    8. dialog.setTextValue(text);
    9. dialog.setTextEchoMode(mode);
    10.  
    11. int ret = dialog.exec();
    12. if (ok)
    13. *ok = !!ret;
    14. if (ret) {
    15. return dialog.textValue();
    16. } else {
    17. return QString();
    18. }
    19. }
    To copy to clipboard, switch view to plain text mode 
    My question is about line 13. First I thought !! is used to convert int to bool, but soon I realized the conversion takes place automatically. Is it something special about !! ?

  2. #2
    Join Date
    Dec 2012
    Posts
    90
    Thanks
    5
    Thanked 20 Times in 18 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: C++ Question

    I don't know for shure, but I guess it is here to catch reader's attention, that a conversion is taking place.
    AFAIK !! doesn't have any special meaning in C++.

    EDIT: OK, I did additional research, it seems to be C idiom of clamping result to the range 0,1. So if it was not zero it becomes 1, if it was zero it stays zero, so that variable will be always either 1 or 0.
    I think it have no meaning here, just a C leftover and a point of attention.
    Last edited by lanz; 1st March 2013 at 08:45.

  3. The following user says thank you to lanz for this useful post:

    Ashkan_s (1st March 2013)

  4. #3
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: C++ Question

    Quote Originally Posted by lanz View Post
    I think it have no meaning here, just a C leftover and a point of attention.
    I hope it is not just a leftover! I makes perfect sense to restrict the value to 0 or 1 here, because it is passed to a bool pointer. Since you do not know how a bool is implemented on the different system, it could be a serious issue if ret would be any other number than 0,1.
    But you are right, right now it is useless because QDialog::DialogCode has only 1 an 0. But if one decide to add an other dialog code in future, this code is still valid and safe (and more important: there is no danger that it could be overseen to change!)

  5. The following user says thank you to Lykurg for this useful post:

    Ashkan_s (1st March 2013)

  6. #4
    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: C++ Question

    It's not being passed to a bool pointer but to a bool variable. bool in C++ doesn't take "1" or "0" as a value, it only accepts "true" and "false" (lowercase). If you give it an integer, the compiler converts it to true or false with false being "0" and true being everything else. I admit it is the first time I see a "!!" construct, it looks like a double negation operator that indeed might be preventing some badly written compilers to wreck havok in your program (maybe assuming that either the compiler thinks !0 != 1 (x)or !1 != 0).
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. Question about Qt+VTK...
    By Patrick Sorcery in forum Qt Programming
    Replies: 0
    Last Post: 11th October 2010, 11:20
  2. few question about skd 3rd v1.1
    By estel in forum Newbie
    Replies: 3
    Last Post: 2nd July 2009, 07:08
  3. MVC question
    By ^NyAw^ in forum Qt Programming
    Replies: 5
    Last Post: 17th June 2009, 12:11
  4. Question
    By Dumbledore in forum Qt Programming
    Replies: 1
    Last Post: 20th October 2007, 23:12
  5. VTK question
    By magland in forum General Programming
    Replies: 0
    Last Post: 9th May 2007, 20:47

Tags for this Thread

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.