Thread: const-ness explanation

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    630

    const-ness explanation

    Hello

    Suppose I have:

    Code:
    std::string const& name() const { return m_name; }
    The second const means I shouldnt do any 'changes' inside name function. Is this right?
    What is the first const for? It means that there shouldnt not be any modifications done on returned reference to string?

    Thanks for help

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Yes, the first const means that the string should not be allowed to be modified by the calling code. As long as the compiler understands it, it will give warning/error if the result from this function is modified.

    --
    Mats

  3. #3
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    I thought putting const after a type (I've only used this with pointers myself) meant that the pointer couldn't be changed, whereas putting const before the type meant the value of the data couldn't be changed?

    Then I've also seen code that was super paranoid about any changes being made
    Code:
    void func( const char* const  param )...

  4. #4
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    const can go on either side of the type. What matters is the side of * or &.

    gg

  5. #5
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    OK, I was thinking it was something like that...
    So const char* and char const* are the same thing then?
    The second way just looks strange (and potentially confusing if you look at it quickly).

  6. #6
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    The trick I was taught was to "read" it from right to left.
    So "pointer to character that's contant" is the same as "point to constant character".

    gg

  7. #7
    The larch
    Join Date
    May 2006
    Posts
    3,573
    As references themselves are already constant (can't be reassigned to reference something else), the const keyword can only mean that what is referenced is constant.

    In the case of the name function, this is not about being paranoid. It wouldn't compile if the return type weren't constant. Because the member function is constant, all the members of the class are constant within this function. Now you can't take a non-constant reference of a constant.

    Or, the member function which has promised not to change the class hands out a string member itself (via reference). If the reference weren't constant, part of the class would be now exposed to be modified from outside through the "fault" of the method which provided the reference.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  8. #8
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    I had to re-read your post a couple times before I realized you said "right to left" not "left to right"

    Yeah, I guess that helps (when you're not skimming through the code quickly).

    const char* = A pointer to a char that is const (char can't be changed).
    char const* = A pointer to a const char (char can't be changed).
    char* const = A const pointer to a char (pointer can't be changed).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Polynomials and ADT's
    By Emeighty in forum C++ Programming
    Replies: 20
    Last Post: 08-19-2008, 08:32 AM
  2. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 11:45 AM
  3. Drawing Program
    By Max_Payne in forum C++ Programming
    Replies: 21
    Last Post: 12-21-2007, 05:34 PM
  4. Certain functions
    By Lurker in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2003, 01:26 AM
  5. Half-life SDK, where are the constants?
    By bennyandthejets in forum Game Programming
    Replies: 29
    Last Post: 08-25-2003, 11:58 AM