By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,465 Members | 1,448 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,465 IT Pros & Developers. It's quick & easy.

const follow the member function

P: n/a
Hello,

I was reading the C++ codes and was confused by the following:

double expect (double t, double x, double dt) const {

return x + t*dt;

}

Does the "const" in the member function follow the function name mean
the returned double cannot be modified?

Rookie question, thanks for your help in advance.

Jul 30 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
Stu
avier wrote:
Hello,

I was reading the C++ codes and was confused by the following:

double expect (double t, double x, double dt) const {

return x + t*dt;

}

Does the "const" in the member function follow the function name mean
the returned double cannot be modified?

Rookie question, thanks for your help in advance.


It means that the member function does not modify the class object it is a
member of.

Stu
Jul 30 '05 #2

P: n/a
hi stu,

thanks for the prompt help.

Does it mean that the data in the object will not be modified?

Jul 30 '05 #3

P: n/a
avier wrote:
hi stu,

thanks for the prompt help.

Does it mean that the data in the object will not be modified?


It does not.

const for member function is just a hint to the reader that the
function does not change logical state of an object, although object's
physical state (the bits it's comprised of) may well change.

Jul 30 '05 #4

P: n/a

avier wrote:
Hello,

I was reading the C++ codes and was confused by the following:

double expect (double t, double x, double dt) const {

return x + t*dt;

}

Does the "const" in the member function follow the function name mean
the returned double cannot be modified?

Rookie question, thanks for your help in advance.


read:
http://www.parashift.com/c++-faq-lit...rrectness.html
sections 18.10-18.13 in particular

Jul 30 '05 #5

P: n/a
Again, thanks Maxim and Alipha!

Jul 30 '05 #6

P: n/a
"Maxim Yegorushkin" <ma***************@gmail.com> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com
avier wrote:
hi stu,

thanks for the prompt help.

Does it mean that the data in the object will not be modified?


It does not.

const for member function is just a hint to the reader that the
function does not change logical state of an object, although object's
physical state (the bits it's comprised of) may well change.


It is far more than a hint.

1. It is a promise that the function won't modify the object's data unless
that data is declared mutable --- a promise that the compiler will enforce.
2. It is a required promise for any function that is called on a const
object --- a requirement that, once again, the compiler will enforce.

For 1., observe that the following won't compile because Set changes the
object in spite of promising not to:

class Test
{
int x;
public:
Test() : x(0)
{}
void Set(int x_) const
{
x = x_; // won't compile; violates the const promise
}
};

For 2., observe that the following won't compile because of the absence of
the const qualifier on the DoNothing function, i.e., the fact that the
DoNothing function does nothing is insufficient; it must declare this fact
with the const keyword:

class Test
{
public:
Test()
{}
void DoNothing()
{}
};

int main()
{
const Test t;
t.DoNothing(); //won't compile; t is declared const but DoNothing isn't
return 0;
}
--
John Carson

Jul 31 '05 #7

P: n/a
thanks, it really hepls!

Aug 4 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.