Results 1 to 13 of 13

Thread: Why const can disappear at QMap::value(...)

Hybrid View

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

    Default Re: Why const can disappear at QMap::value(...)

    So I can use MyObject * object_one = myMap->value(1); and it is completely valid, although value() returns MyObject const * object_one (which is a copy of the value in the map).
    In your example the QMap<Key,T> template works out like this
    Qt Code:
    1. QMap<int, MyObject*> map;
    2. // ^ ^
    3. // | +-- T == "MyObject*"
    4. // |
    5. // +-- Key == "int"
    To copy to clipboard, switch view to plain text mode 
    The function QMap::value() returns a "const T", that is, "const MyObject*" (constant pointer-to-obj) not "MyObject const *" (pointer-to-const-obj) . In assigning that return value to a pointer variable you make a copy of it, which may or may not be const.

    Have a look at the variations in here:
    Qt Code:
    1. #include <QCoreApplication>
    2. #include <QMap>
    3.  
    4. class MyObject {
    5. public:
    6. void constFunc() const { }
    7. void nonConstFunct() { }
    8. };
    9.  
    10. int main(int argc, char **argv) {
    11. QCoreApplication app(argc, argv);
    12.  
    13. MyObject *a = new MyObject;
    14. MyObject const *b = new MyObject;
    15.  
    16. QMap<int, MyObject *> map1;
    17. map1.insert(0, a); // ok
    18. // map1.insert(1, b); // error, cannot put pointer-to-const in map
    19.  
    20. MyObject *c = map1.value(0); // ok
    21. MyObject const *d = map1.value(0); // ok
    22. c->nonConstFunct(); // ok
    23. // d->nonConstFunct(); // error, we promised d pointed to a const object
    24.  
    25. // You can have a map contain pointer-to-const-object
    26. QMap<int, MyObject const *> map2;
    27. map2.insert(0, a); // ok
    28. map2.insert(1, b); // ok
    29.  
    30. // c = map2.value(0); // error, map cannot put "MyObject const *" into a non-const pointer var
    31. d = map2.value(1); // ok
    32.  
    33. d->constFunc(); // ok, pointer-to-const and const function
    34. // d->nonConstFunct(); // error, pointer-to-const and non-const function
    35.  
    36. delete a;
    37. delete b;
    38.  
    39. return 0;
    40. }
    To copy to clipboard, switch view to plain text mode 

  2. The following 2 users say thank you to ChrisW67 for this useful post:

    d_stranz (18th December 2015), QtCrawler (18th December 2015)

  3. #2
    Join Date
    Nov 2015
    Posts
    17
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows
    Thanks
    9

    Default Re: Why const can disappear at QMap::value(...)

    Ok thanks for your reply!

    I thought const MyObject* is the same as MyObject const *, so a pointer to a const MyObject. (I read it from right to left.)
    Last edited by QtCrawler; 18th December 2015 at 13:03.

  4. #3
    Join Date
    May 2015
    Posts
    66
    Qt products
    Qt5
    Platforms
    Windows
    Thanks
    10
    Thanked 17 Times in 17 Posts

    Default Re: Why const can disappear at QMap::value(...)

    Quote Originally Posted by ChrisW67 View Post
    "const MyObject*" (constant pointer-to-obj) not "MyObject const *" (pointer-to-const-obj)
    @ChrisW67 I don't think so..

    If I am not wrong, "MyObject * const" is a constant pointer-to-obj. The two things you mentioned are both pointers to const-obj.

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

    QtCrawler (18th December 2015)

  6. #4
    Join Date
    Oct 2009
    Posts
    483
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Thanked 97 Times in 94 Posts

    Default Re: Why const can disappear at QMap::value(...)

    Quote Originally Posted by Vikram.Saralaya View Post
    @ChrisW67 I don't think so..

    If I am not wrong, "MyObject * const" is a constant pointer-to-obj. The two things you mentioned are both pointers to const-obj.
    Indeed. ChrisW67 textually substituted "T" with "MyObject*" is his otherwise perfectly exact explanation, but I am sure he meant a semantic substitution: "immutable T" -> "immutable pointer to (mutable) MyObject" aka "MyObject *const".

  7. The following 2 users say thank you to yeye_olive for this useful post:

    d_stranz (18th December 2015), QtCrawler (18th December 2015)

  8. #5
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,345
    Qt products
    Qt5
    Platforms
    Windows
    Thanks
    318
    Thanked 872 Times in 859 Posts

    Default Re: Why const can disappear at QMap::value(...)

    Even after 25+ years of C++ coding, this is one aspect that continues to confuse me. And then there are the versions with two const declarations "const MyObject const *" or whatever. Thank god for google.

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

    Default Re: Why const can disappear at QMap::value(...)

    It is a strange person that does not have to think twice (i wrote the code to check myself). As yeye_olive points out, even my explanation is a little imprecise. To be clear, value() returns type MyObject* const and not const MyObject* (more clearly written MyObject const *) as a naive text substitution interpretation of templates might lead you to think.

    In my defence, it was a long week

  10. The following user says thank you to ChrisW67 for this useful post:

    QtCrawler (18th December 2015)

  11. #7
    Join Date
    Nov 2015
    Posts
    17
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows
    Thanks
    9

    Default Re: Why const can disappear at QMap::value(...)

    Thank you guys.
    It's great to discuss such topics with you.
    Thank god for QtC

  12. #8
    Join Date
    Oct 2009
    Posts
    483
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Thanked 97 Times in 94 Posts

    Default Re: Why const can disappear at QMap::value(...)

    Quote Originally Posted by d_stranz View Post
    Even after 25+ years of C++ coding, this is one aspect that continues to confuse me. And then there are the versions with two const declarations "const MyObject const *" or whatever. Thank god for google.
    C/C++ is quite liberal with the placement of the "const" qualifier in a declarator. IMHO it is a shame that the ambiguous form "const T *" be so prevalent. I tend to consistently place "const" after the type it qualifies, like * and &, which eliminates the ambiguous forms. All I have to do is read the declaration from right to left:
    T const: constant T
    T const *: pointer to constant T
    T * const: constant pointer to T
    T const * const: constant pointer to constant T

    If you ever are in doubt about a declaration, check out the excellent cdecl.org.

  13. The following user says thank you to yeye_olive for this useful post:

    d_stranz (19th December 2015)

Similar Threads

  1. Replies: 0
    Last Post: 22nd June 2013, 11:02
  2. screen just disappear after compiling
    By calebmakore in forum Qt Programming
    Replies: 1
    Last Post: 5th October 2011, 09:20
  3. Can't get title bar to disappear from widget
    By MattPhillips in forum Qt Programming
    Replies: 11
    Last Post: 2nd November 2010, 15:41
  4. Replies: 1
    Last Post: 4th December 2009, 18:03
  5. const member and const method
    By mickey in forum General Programming
    Replies: 8
    Last Post: 9th April 2008, 10:44

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
  •  
Qt is a trademark of The Qt Company.