Thread: Whats the difference between

  1. #1
    Registered User
    Join Date
    Jun 2007
    Posts
    219

    Whats the difference between

    Whats the difference between
    Code:
    const void X::y(){
      //I think it assure's that even if it returns a pointer or a refrence it cant be changed
      //such that the pointee or referenced item will be changed
      //Plese Correct me If I am wrong
    }
    and
    Code:
    void X::y() const{
      //Does not alter any data of the class
        //so can't call any non-const member function too
      //Plese Correct me If I am wrong
    }
    Edit: forgot to add a return Type.
    So now is my Comments Correct ?? I think Wrong..
    Last edited by noobcpp; 07-22-2008 at 03:38 AM.

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    const void ... doesn't make any sense [although it may well be valid as far as the compiler is concerned].

    const char * for example means "you'll get a pointer to char, but damn you if you change any of it". It prevents your code from destroying data that isn't yours, and allows the compiler to make assumptions that it can't make if you have a regular char *.

    const before the { means "I'm not going to change any of the contents of the class in this function" - it serves two purposes:
    1. You tell others using this member function that nothing in the object will be changed.
    2. You tell the compiler that it can take short-cuts in calling this function, as it doesn't have to reload data from the object itself after the call, as nothing should have changed. [Whether the compiler understands and makes use of this or not is a different matter].

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    May 2008
    Location
    Paris
    Posts
    248
    I would like to add a third purpose: it allows the return value of your function to be used as argument of another function, when that function's argument is declared as a 'const' .

    Code:
    T operator[](int i);
    void set(T const& obj);
    should be supplemented with
    Code:
    T& operator[](int i) const;

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Do you mean:
    Code:
    const T& operator[](int i) ;
    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Registered User
    Join Date
    May 2008
    Location
    Paris
    Posts
    248
    oops.... my mistake

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Of course, if the operator[] is ONLY returning either a copy of the content, or a const reference, then there should probably be a const at the back of the function too - because it doesn't change anything of the objects content. That is of course only true if it isn't doing something like "add a new object in the list of objects if there isn't one with the current index i", etc..

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #7
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    Quote Originally Posted by matsp View Post
    2. You tell the compiler that it can take short-cuts in calling this function, as it doesn't have to reload data from the object itself after the call, as nothing should have changed. [Whether the compiler understands and makes use of this or not is a different matter].
    I think you're thinking of volatile.

  8. #8
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by robwhit View Post
    I think you're thinking of volatile.
    Nope, quite the opposite. The compiler can when the member function is const, pass an object to a function, whilst keeping a value from a member variable of the object in a register across the function call. I'm not sure any compiler actually implements this, but the point is that the object MUST not change in such a function call [unless you do stuff like const_cast<T *>(this)], so the compiler should be able to rely on the object not changing.

    I'm of course not sure any compiler actually does this.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  9. #9
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    Oh! Right. I was thinking of regular variables and stuff.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Review required for program of date difference
    By chottachatri in forum C Programming
    Replies: 6
    Last Post: 10-31-2008, 11:46 AM
  2. Difference Equations / Recurrence Relations
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 10-05-2007, 10:26 AM
  3. What's the difference between var++ and ++var
    By ulillillia in forum C Programming
    Replies: 6
    Last Post: 05-31-2007, 02:27 AM
  4. Difference between macro and pass by reference?
    By converge in forum C++ Programming
    Replies: 2
    Last Post: 02-26-2002, 05:20 AM
  5. how to get difference of digits
    By Leeman_s in forum C++ Programming
    Replies: 5
    Last Post: 12-20-2001, 08:32 PM