Function Definition

This is a discussion on Function Definition within the C++ Programming forums, part of the General Programming Boards category; I was wondering what the "const" after the function definition means. Not the first one i.e the type but the ...

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    8

    Question Function Definition

    I was wondering what the "const" after the function definition means. Not the first one i.e the type but the second one (outside the brackets).

    This code was inside a class if that means anything.

    Code:
     const string& getSomething() const { return something; }
    what is this -------------------------------------------------^^^^

    Thanks,
    CorX

  2. #2
    Making mistakes
    Join Date
    Dec 2008
    Posts
    476
    It means this function won't modify any attribute of the instance that is not declared as mutual. Only such functions can be called within constant instances. The same with volatile.

  3. #3
    Registered User
    Join Date
    Mar 2009
    Posts
    8
    Thanks, I understand now.

  4. #4
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,246
    It means this function won't modify any attribute of the instance that is not declared as mutual
    I think you meant mutable.

    Only such functions can be called within constant instances
    No, they can be called from constant and non-constant class instances. A const instance can only call const functions, whereas a non-const instance can call either.

    The same with volatile
    Volatile has nothing to do with const.

  5. #5
    Making mistakes
    Join Date
    Dec 2008
    Posts
    476
    Ooops... Was thinking about mutually-recursive acronyms...

    1. This means that "only" functions declared const can be called from constant instances. It doesn't mean they can't be called from other ones.

    2. Volatile means volatile instances can be passed to this method as this.

  6. #6
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,230
    Quote Originally Posted by bithub View Post
    Volatile has nothing to do with const.
    Volatile and const are intimately related. Anywhere that const may syntactically appear, volatile may appear. And like const, the only way to expunge volatile from a type is by using const_cast<>.

    There is a reason why const and volatile are referred to as "CV qualifiers." C being Const, V being Volatile.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  7. #7
    Registered User linuxdude's Avatar
    Join Date
    Mar 2003
    Location
    Louisiana
    Posts
    926
    What does this const actually mean again? I don't know what it does.
    any attribute of the instance
    is a vague statement to me.

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,311
    "Any attribute of the instance" can be interpreted to mean "any member variable of the object".
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    Registered User linuxdude's Avatar
    Join Date
    Mar 2003
    Location
    Louisiana
    Posts
    926
    Ok, so it's a guarantee that member variables not defined as mutable aren't changed.
    Code:
    class s {
      public:
       void func (void) const {
          x = 1;
       }
      private:
       mutable int x;
    };
    Works fine, but
    Code:
    class s {
      public:
       void func (void) const {
          x = 1;
       }
      private:
       int x;
    };
    produces a
    Code:
    error: assignment of data-member s::x in read-only structure
    Thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Beginner Needs help in Dev-C++
    By Korrupt Lawz in forum C++ Programming
    Replies: 20
    Last Post: 09-28-2010, 01:17 AM
  2. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 11:45 AM
  3. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 04:45 PM
  4. load gif into program
    By willc0de4food in forum Windows Programming
    Replies: 14
    Last Post: 01-11-2006, 09:43 AM
  5. structure vs class
    By sana in forum C++ Programming
    Replies: 13
    Last Post: 12-02-2002, 06:18 AM

Tags for this Thread


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