Thread: Function Definition

  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,268
    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
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    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
    28,413
    "Any attribute of the instance" can be interpreted to mean "any member variable of the object".
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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, 05:45 PM
  4. load gif into program
    By willc0de4food in forum Windows Programming
    Replies: 14
    Last Post: 01-11-2006, 10:43 AM
  5. structure vs class
    By sana in forum C++ Programming
    Replies: 13
    Last Post: 12-02-2002, 07:18 AM

Tags for this Thread