C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

const follow the member function

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++)
View previous topic :: View next topic  
Author Message
avier
Guest





PostPosted: Sat Jul 30, 2005 10:44 am    Post subject: const follow the member function Reply with 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.

Back to top
Stu
Guest





PostPosted: Sat Jul 30, 2005 11:06 am    Post subject: Re: const follow the member function Reply with quote



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





PostPosted: Sat Jul 30, 2005 11:26 am    Post subject: Re: const follow the member function Reply with quote



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





PostPosted: Sat Jul 30, 2005 12:29 pm    Post subject: Re: const follow the member function Reply with quote

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





PostPosted: Sat Jul 30, 2005 5:25 pm    Post subject: Re: const follow the member function Reply with quote


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





PostPosted: Sat Jul 30, 2005 7:06 pm    Post subject: Re: const follow the member function Reply with quote

Again, thanks Maxim and Alipha!

Back to top
John Carson
Guest





PostPosted: Sun Jul 31, 2005 3:01 am    Post subject: Re: const follow the member function Reply with quote

"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





PostPosted: Thu Aug 04, 2005 11:16 am    Post subject: Re: const follow the member function Reply with quote

thanks, it really hepls!

Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++) All times are GMT
Page 1 of 1

 
Jump to:  
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


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.