 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
avier Guest
|
Posted: Sat Jul 30, 2005 10:44 am Post subject: const follow the member function |
|
|
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.
|
|
| Back to top |
|
 |
Stu Guest
|
Posted: Sat Jul 30, 2005 11:06 am Post subject: Re: const follow the member function |
|
|
avier wrote:
| Quote: | 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
|
|
| Back to top |
|
 |
avier Guest
|
Posted: Sat Jul 30, 2005 11:26 am Post subject: Re: const follow the member function |
|
|
hi stu,
thanks for the prompt help.
Does it mean that the data in the object will not be modified?
|
|
| Back to top |
|
 |
Maxim Yegorushkin Guest
|
Posted: Sat Jul 30, 2005 12:29 pm Post subject: Re: const follow the member function |
|
|
avier wrote:
| Quote: | 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.
|
|
| Back to top |
|
 |
Alipha Guest
|
Posted: Sat Jul 30, 2005 5:25 pm Post subject: Re: const follow the member function |
|
|
avier wrote:
| Quote: | 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-lite/const-correctness.html
sections 18.10-18.13 in particular
|
|
| Back to top |
|
 |
avier Guest
|
Posted: Sat Jul 30, 2005 7:06 pm Post subject: Re: const follow the member function |
|
|
Again, thanks Maxim and Alipha!
|
|
| Back to top |
|
 |
John Carson Guest
|
Posted: Sun Jul 31, 2005 3:01 am Post subject: Re: const follow the member function |
|
|
"Maxim Yegorushkin" <maxim.yegorushkin (AT) gmail (DOT) com> wrote
| Quote: | 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
|
|
| Back to top |
|
 |
avier Guest
|
Posted: Thu Aug 04, 2005 11:16 am Post subject: Re: const follow the member function |
|
|
thanks, it really hepls!
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|