Results 1 to 13 of 13

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

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Nov 2015
    Posts
    17
    Thanks
    9
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

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

    Hello,

    I have some basic questions, I'm very interested in.

    If I have a QMap<int,MyObject*>* and call method value on it, the method returns a pointer to a const instance of MyObject.
    Qt Code:
    1. QMap<int,MyObject*>* myMap = new QMap<int,MyObject*>();
    2. myMap->insert(1,new MyObject());
    3.  
    4. MyObject const * object_one = myMap->value(1); // const instance of myObject. I'm not allowed to call non-const methods on it.
    5. object_one->changeMe(); //compiler will complain about it
    To copy to clipboard, switch view to plain text mode 

    but I can write it like this:
    Qt Code:
    1. MyObject * object_one = myMap->value(1); // non-const instance of myObject. I'm allowed to call non-const methods on it.
    2. object_one->changeMe(); //compiler wont't complain about it
    To copy to clipboard, switch view to plain text mode 

    And now I can mutate the instance. I want to know if this works just because of c-compatibility, if it's ok to use it like that (I would say no)?

    Further I want to know how should I pass such an instance to another object which should hold the instance as mutable member (MyObject* myobject):

    Qt Code:
    1. OtherClass(MyObject * myobject) : myobject(myobject){ }
    2.  
    3. MyObject const * const_object_one = myMap->value(1); // const instance of myObject. I'm not allowed to call non-const methods on it.
    4. MyObject * object_one = myMap->value(1); // non-const instance of myObject. I'm allowed to call non-const methods on it.
    5.  
    6. OtherClass* other_object = new OtherClass(const_object_one);//fails
    7.  
    8. OtherClass* other_object = new OtherClass(object_one);//works
    To copy to clipboard, switch view to plain text mode 

    Can I call the constructor passing the object via QMap::value, or should I use []->operator?

    Thank you
    Last edited by QtCrawler; 17th December 2015 at 08:18.

Similar Threads

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