Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

question about const

This topic is 5399 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Advertisement
"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.

Share this post


Link to post
Share on other sites
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...

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites

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]

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!