Can someone explain the use of 'const' in this case

This is a discussion on Can someone explain the use of 'const' in this case within the C++ Programming forums, part of the General Programming Boards category; I was reading my C++ book for class, but it didn't tell how, or why const was used in this ...

  1. #1
    Registered User
    Join Date
    Aug 2003
    Posts
    13

    Can someone explain the use of 'const' in this case

    I was reading my C++ book for class, but it didn't tell how, or why const was used in this case:

    Code:
    bool String:: operator!() const {
       return length == 0;
    }
    Thanks!

    Edit: Excuse the space between ":: operator" I had to put it there or the BB thought I was trying to use a smiley. lol

  2. #2
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    'const' on a member function like that is a promise that the function will not change the object to which it belongs (that is, it makes no modifications to '*this'). The one exception is that it can modify member data declared to be 'mutable'.
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  3. #3
    Registered User
    Join Date
    Aug 2003
    Posts
    13
    What does *this mean, I haven't seen it before?

    I just looked up mutable. Basically, anti-const.

  4. #4
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,659
    Originally posted by steve_demaio
    What does *this mean, I haven't seen it before?

    I just looked up mutable. Basically, anti-const.
    Well this is a pointer to the class (not the class itself, but each and every object of a class has a 'this' pointer). So *this is dereferencing the pointer (just as you might any other kind of pointer).

    >>Edit: Excuse the space between ":: operator" I had to put it there or the BB thought I was trying to use a smiley. lol

    You can disable smilies in a post by checking off the corresponding box below the edit-control you type your reply in...



    ITSA
    Socket Library!

  5. #5
    Registered User
    Join Date
    Aug 2003
    Posts
    13
    Ahh, thank you. That's much clearer now. Here is one scenario though.

    Take this function:
    Code:
    double Complex::realPart() const{                 
      cout << *this << endl;
      return rp;
    }
    *this refers to rp in this case right?

  6. #6
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    I don't think it is possible for *this to refer to rp...I assume rp is private data:
    Code:
    class Complex
    {
    public:
    //...
    double realPart() const;
    
    private:
    double rp;
    };
    
    
    double Complex::realPart() const
    {
    cout<<*this<<endl; //operator << must be overloaded for const Complex&
    return rp;
    }
    
    int main()
    {
    Complex myComplex;
    double x=myComplex.realPart();
    }
    In that case *this would refer to the myComplex object...
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 03-05-2009, 10:32 AM
  2. Code review
    By Elysia in forum C++ Programming
    Replies: 71
    Last Post: 05-13-2008, 09:42 PM
  3. Intel syntax on MinGW ?
    By TmX in forum Tech Board
    Replies: 2
    Last Post: 01-06-2007, 08:44 AM
  4. Xmas competitions
    By Salem in forum Contests Board
    Replies: 88
    Last Post: 01-03-2004, 01:08 PM
  5. error with code
    By duffy in forum C Programming
    Replies: 8
    Last Post: 10-22-2002, 09:45 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21