![]() |
| | #1 |
| Registered User Join Date: Mar 2009
Posts: 8
| This code was inside a class if that means anything. Code: const string& getSomething() const { return something; }
Thanks, CorX |
| CorX is offline | |
| | #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 | |
| | #3 |
| Registered User Join Date: Mar 2009
Posts: 8
| Thanks, I understand now. |
| CorX is offline | |
| | #4 | |||
| Registered User Join Date: Sep 2004 Location: California
Posts: 2,845
| Quote:
Quote:
Quote:
| |||
| bithub is offline | |
| | #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 | |
| | #6 |
| Senior software engineer Join Date: Mar 2007 Location: Portland, OR
Posts: 5,381
| 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 | |
| | #7 | |
| Registered User Join Date: Mar 2003 Location: Louisiana
Posts: 926
| What does this const actually mean again? I don't know what it does. Quote:
| |
| linuxdude is offline | |
| | #8 |
| C++ Witch 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 | |
| | #9 |
| Registered User 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;
};
Code: class s {
public:
void func (void) const {
x = 1;
}
private:
int x;
};
Code: error: assignment of data-member ‘s::x’ in read-only structure |
| linuxdude is offline | |
![]() |
| Tags |
| c++, class, constant, definition, function |
| Thread Tools | |
| Display Modes | |
|
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 |