Results 1 to 9 of 9

Thread: const member and const method

  1. #1
    Join Date
    Jan 2006
    Posts
    976
    Thanks
    53
    Qt products
    Qt3
    Platforms
    Windows

    Default const member and const method

    Hello,
    today I learnt this:
    Qt Code:
    1. class A {
    2. int* _value;
    3. public:
    4. A (int value) : _value (new int(value)) {}
    5. ~A() { delete _value; }
    6. void redouble () const { *_value *= 2; } // const method can modified the value pointed by _value
    7. };
    To copy to clipboard, switch view to plain text mode 
    And it isn't what I want: is there a way treat this thing, please
    Regards

  2. #2
    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: const member and const method

    Keep a reference instead of a pointer.

  3. #3
    Join Date
    Jan 2006
    Posts
    976
    Thanks
    53
    Qt products
    Qt3
    Platforms
    Windows

    Default Re: const member and const method

    Quote Originally Posted by wysota View Post
    Keep a reference instead of a pointer.
    Why do you suggest a reference? Because a pointer like this isn't good (then I can take a simple int) !?
    Qt Code:
    1. int & _value
    2. A (int value = 0) : _value (value) {}
    To copy to clipboard, switch view to plain text mode 
    An if I had the sistuation here above (where the constructor cant take nothing) ? here, what I have to assign in the constructor? ('0' for pointers means 'nothing', then for "references" what means "nothing"?)
    Regards

  4. #4
    Join Date
    Oct 2006
    Posts
    279
    Thanks
    6
    Thanked 40 Times in 39 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: const member and const method

    Quote Originally Posted by mickey View Post
    An if I had the sistuation here above (where the constructor cant take nothing) ? here, what I have to assign in the constructor? ('0' for pointers means 'nothing', then for "references" what means "nothing"?)
    You can't do that. References don't allow reseating, and thus no late initialization.

    If you want to keep using pointers remember that
    Qt Code:
    1. const int* _value;
    To copy to clipboard, switch view to plain text mode 
    means a const pointer to a non const object. I.e. you can change the pointee, but not the pointer. If you want a pointer, where you can't change the value, you need to declare it:
    Qt Code:
    1. int const *_value;
    To copy to clipboard, switch view to plain text mode 
    But then you can't change the value in a non const method either.

  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: const member and const method

    A solution might be to have a wrapper object around the pointer that would only allow accessing the pointed object in a non-const way. Something like a shared data pointer.

  6. #6
    Join Date
    Jan 2006
    Posts
    976
    Thanks
    53
    Qt products
    Qt3
    Platforms
    Windows

    Default Re: const member and const method

    I don't understand if this behaviour of C++ (const methods on pointer data, I mean), is right or it's something similar to an accepted bug...
    In general if I had a situation like that isn't dangerous to have a method const that isn't really const at all?
    Regards

  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: const member and const method

    The behaviour you observe is correct. Object pointed to by a pointer is not part of the object containing the pointer, thus changing it from a const method of the object is allowed. It's exactly the same as you would change a global (or any other external) object.

  8. #8
    Join Date
    Jan 2006
    Posts
    976
    Thanks
    53
    Qt products
    Qt3
    Platforms
    Windows

    Default Re: const member and const method

    Quote Originally Posted by wysota View Post
    The behaviour you observe is correct. Object pointed to by a pointer is not part of the object containing the pointer, thus changing it from a const method of the object is allowed. It's exactly the same as you would change a global (or any other external) object.
    And are these two statics part of class?
    Qt Code:
    1. class A {
    2. static const int _size = 100; //1
    3. static int rom; //2
    4. }
    5. int A::rom = 10;
    To copy to clipboard, switch view to plain text mode 
    Regards

  9. #9
    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: const member and const method

    Part of class - yes. Part of object - no. Thus probably (not sure about it) changing "rom" from a const method is allowed (the other variable is const, so you won't be able to modify it).

Similar Threads

  1. problem with forward declaration
    By MarkoSan in forum General Programming
    Replies: 14
    Last Post: 6th January 2008, 21:45
  2. Qtopia core 4.2.2 cross compile make error
    By smiyai18 in forum Installation and Deployment
    Replies: 2
    Last Post: 28th August 2007, 17:04
  3. use qpsql
    By raphaelf in forum Installation and Deployment
    Replies: 34
    Last Post: 22nd August 2006, 12:52
  4. emiting signals from const member functions !?
    By sunil.thaha in forum Qt Programming
    Replies: 2
    Last Post: 25th March 2006, 11:29
  5. const member definitions in a class ....
    By TheKedge in forum General Programming
    Replies: 5
    Last Post: 15th February 2006, 13:03

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.