C Board  

Go Back   C Board > General Programming Boards > C++ Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 05-12-2009, 09:16 AM   #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
CorX is offline   Reply With Quote
Old 05-12-2009, 09:29 AM   #2
Making mistakes
 
Join Date: Dec 2008
Posts: 347
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.
Brafil is offline   Reply With Quote
Old 05-12-2009, 09:36 AM   #3
Registered User
 
Join Date: Mar 2009
Posts: 8
Thanks, I understand now.
CorX is offline   Reply With Quote
Old 05-12-2009, 09:40 AM   #4
Registered User
 
Join Date: Sep 2004
Location: California
Posts: 2,845
Quote:
It means this function won't modify any attribute of the instance that is not declared as mutual
I think you meant mutable.

Quote:
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.

Quote:
The same with volatile
Volatile has nothing to do with const.
bithub is offline   Reply With Quote
Old 05-12-2009, 09:54 AM   #5
Making mistakes
 
Join Date: Dec 2008
Posts: 347
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.
Brafil is offline   Reply With Quote
Old 05-12-2009, 10:28 AM   #6
Senior software engineer
 
brewbuck's Avatar
 
Join Date: Mar 2007
Location: Portland, OR
Posts: 5,381
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.
__________________
"Congratulations on your purchase. To begin using your quantum computer, set the power switch to both off and on simultaneously." -- raftpeople@slashdot
brewbuck is offline   Reply With Quote
Old 05-14-2009, 09:39 AM   #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.
Quote:
any attribute of the instance
is a vague statement to me.
linuxdude is offline   Reply With Quote
Old 05-14-2009, 09:58 AM   #8
C++ Witch
 
laserlight's Avatar
 
Join Date: Oct 2003
Location: Singapore
Posts: 10,354
"Any attribute of the instance" can be interpreted to mean "any member variable of the object".
__________________
C + C++ Compiler: MinGW port of GCC
Build + Version Control System: SCons + Bazaar

Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
laserlight is online now   Reply With Quote
Old 05-14-2009, 11:56 AM   #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
linuxdude is offline   Reply With Quote
Reply

Tags
c++, class, constant, definition, function

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Beginner Needs help in Dev-C++ Korrupt Lawz C++ Programming 18 05-02-2009 12:27 PM
Undefined Reference Compiling Error AlakaAlaki C++ Programming 1 06-27-2008 11:45 AM
We Got _DEBUG Errors Tonto Windows Programming 5 12-22-2006 05:45 PM
load gif into program willc0de4food Windows Programming 14 01-11-2006 10:43 AM
structure vs class sana C++ Programming 13 12-02-2002 07:18 AM


All times are GMT -6. The time now is 12:09 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

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