question about const

Started by
14 comments, last by amish1234 20 years, 8 months ago
What does it mean when const is used in the following way?

int TheFunction() const
{
   return 0;
}
___________________________________________________________Where to find the intensity (Updated Dec 28, 2004)Member of UBAAG (Unban aftermath Association of Gamedev)
Advertisement
*clips answer which he said could be wrong, and ended up being wrong*

[edited by - MaulingMonkey on August 7, 2003 12:19:22 AM]
"int blah() const" has to be a member of some class. The const means that the function won''t modify the contents of the object on which it operates.
My stuff.Shameless promotion: FreePop: The GPL god-sim.
say you have an operator+ for a complex number class...

Complex a, b, c;
c = a+b;

a and b aren''t modified. a''s method operator+ returns a temp object that gets assigned to c. operator+ (...) const is used to signify that the function won''t modify a''s member variables. I forget exactly when this is required...
quote:Original post by Nypyren
I forget exactly when this is required...


Say you have a constant that gets promoted to your class. Eg. a string:

string a = " world";
string x = "hello" + a;

"hello" would be promoted to a constant string type, so operator+ must be a const member.

Image loads when I''m online!The following statement is true. The previous statement is false.
Shameless promotion:
FreePop: The GPL Populous II clone.
My stuff.Shameless promotion: FreePop: The GPL god-sim.
Is it valid for a const function to return a reference to a member variable and then have the reference edited outside of the function?
OpenGL Revolutions http://students.hightechhigh.org/~jjensen/
I would assume so.
A const function could return a pointer, so the value could be changed. I would assume that it could do the same for a reference. But, I could be wrong...


Check out my raytracer at http://simp-raytracer.sourceforge.net. Download it at http://www.sourceforge.net/projects/simp-raytracer.
MyClass& MyClass::returnObject(void)const //WRONG{   return(*this);}// error C2440: 'return' : 'const class MyClass' cannot be // converted to 'class MyClass &'.const MyClass& MyClass::returnObject(void)const //RIGHT{   return(*this);}


If you would be allowed to return a non-const reference your data could be modified despite the member function being const. Thus it's not legal.

[edited by - Wildfire on August 8, 2003 3:09:48 AM]
How do I set my laser printer on stun?
Wildfire, here''s something that''ll surprise you:

struct T {  T(int& b) : a(b) {}  int& a;  int& getA() const {    return a;  }};int main() {  int x = 4, y = 8;  const T t(x);   //note: t is const.  cout << t.a << '','';  t.getA() = y;  cout << t.a;} 

Compiles OK, output is: 4,8
civguy, that''s not the same thing. Wildfire is returning (*this) which is a const variable, so the function''s return type has to be const as well. Your member variable ''int a'' is not const, and you do not modify it inside getA(), so all is well. Also it doesn''t matter that t is const, because you are not [directly] modifying it, you are using an accessor function.

This topic is closed to new replies.

Advertisement