P
Pmb
What does it meant when a function member of a class is declared as const?
Thanks
Pmb
Thanks
Pmb
Pmb said:What does it meant when a function member of a class is declared as const?
Pmb said:What does it meant when a function member of a class is declared as const?
Pmb said:What does it meant when a function member of a class is declared as const?
Sharad Kala said:const?
Always check the FAQ first.
http://www.parashift.com/c++-faq-lite/const-correctness.html#faq-18.10
Pmb said:Thanks. I checked the FAQ and didn't see that. I searched the page for
"const function" and didn't see it.
In any case, after reading it, I don't understand what it means by "The
'abstract (client-visible) state of the object isn't going to change"
Karl Heinz Buchegger said:That the function is not going to change the state of the
object when called.
In practice this means: This function can be called on const objects.
Pmb said:I don't understand!? Take the program below as an example. The output is
===
Object Test constructed with x = 1, y = 2
x in print() is 8
y in print() is 9
===
The object "test" was declared constant and yet I modified the two data
members x, y.
Pmb said:I don't understand!? Take the program below as an example. The output is
===
Object Test constructed with x = 1, y = 2
x in print() is 8
y in print() is 9
===
The object "test" was declared constant and yet I modified the two data
members x, y.
Does that mean that I've changed the value of the object or
the state of the object.
object"?Perhaps I don't understand what is meant above by the "state of the
Pmb said:[snip]
In any case, after reading it, I don't understand what it means by "The
'abstract (client-visible) state of the object isn't going to change"
John Harrison said:No you didn't. In print x and y are parameters they are not the member
variables x and y. You wouldnot be able to change the member variables x and
y, but you can change the parameters x and y because they are not declared
const.
int main()
{
Test const test( 1, 2 );
test.print ( 8, 9 );
return 0;
}
-------------------------------------------------------------------------- -
What does it meant when a function member of a class is declared as
const?
Thanks
Pmb
JKop said:Pmb posted:
int DoStuff(const Dog& doggie)
{
doggie.age = 5;
//ERROR, CANNOT EDIT const OBJECT
return 0;
}
His question is that what is a const member function if you read
carefully.
JKop said:...
Checklist:
1) Does your function edit object member variables? If not, declare it
const .
3) Does your function neither read nor edit member variables? If so, declare
it static .
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.