Results 1 to 7 of 7

Thread: What does "explicit" mean?

  1. #1
    Join Date
    Sep 2008
    Posts
    93
    Thanks
    6
    Thanked 1 Time in 1 Post

    Default What does "explicit" mean?

    I saw this key word "explicit" in some example code. what does it mean in QT?

    Thanks!

  2. #2
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: What does "explicit" mean?

    explicit keywoard is used to avoid implicit type conversion.

  3. #3
    Join Date
    Sep 2008
    Posts
    93
    Thanks
    6
    Thanked 1 Time in 1 Post

    Default Re: What does "explicit" mean?

    Thanks!

    Could you please give an example?

  4. #4
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: What does "explicit" mean?

    example 1 (implicit conversion)
    Qt Code:
    1. class MyObj
    2. {
    3. public:
    4. MyObj(int size): m_size(size) {}
    5. private:
    6. int m_size;
    7. };
    8.  
    9. ....
    10. void A::someMethod()
    11. {
    12. MyObj mw(10);
    13. mw = 5;// wrong; trying convert 5 to MyObj (implicit conversion)
    14. }
    To copy to clipboard, switch view to plain text mode 
    example 2 (using explicit keyword)
    Qt Code:
    1. class MyObj
    2. {
    3. public:
    4. explicit MyObj(int size): m_size(size) {}
    5. private:
    6. int m_size;
    7. };
    8.  
    9. ....
    10. void A::someMethod()
    11. {
    12. MyObj mw(10);
    13. mw = 5;//compile error; explicit conversion is needed.
    14. mw = MyObj(5);//ok
    15. }
    To copy to clipboard, switch view to plain text mode 

  5. #5
    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 does "explicit" mean?

    In another words, if you construct an object and there is no constructor that takes exactly the same types as you are trying to pass, the compiler tries to find conversions from the types you pass to the types there is a constructor for available. The "explicit" keyword prevents that mechanism.

    For example if you have a constructor that takes an integer and you try to pass a double to it, the compiler will do an implicit conversion from double to int and will use the constructor taking an int. If you make that constructor explicit, the compiler will not try to convert your double to int but instead will throw an error.

  6. #6
    Join Date
    Dec 2006
    Posts
    849
    Thanks
    6
    Thanked 163 Times in 151 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: What does "explicit" mean?

    Quote Originally Posted by wysota View Post
    In another words, if you construct an object and there is no constructor that takes exactly the same types as you are trying to pass, the compiler tries to find conversions from the types you pass to the types there is a constructor for available. The "explicit" keyword prevents that mechanism.

    For example if you have a constructor that takes an integer and you try to pass a double to it, the compiler will do an implicit conversion from double to int and will use the constructor taking an int. If you make that constructor explicit, the compiler will not try to convert your double to int but instead will throw an error.
    Sorry to contradict, but not really.
    The explicit means that if class X has a constructor explicit X(T t) for some type T and you call a function that takes an X, but you pass only a T, then this X will not be implicitly constructed from the given T. (The assignment operator being one example of such a function.)
    The example given by spirit illustrates this: you must explicitly construct an instance of X, it will not implicitly convert a constructor's argument (of type T) to an X.

    (Side note: it follows that explicit makes only sense for constructors with one argument, as the others can not be used implicitly anyway.)

  7. #7
    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 does "explicit" mean?

    Yes, you're right. It simply means you have to call the constructor explicitely as a constructor for the item to be constructed, regardless of the types used. My bad.

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.