473,387 Members | 1,530 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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.

Jul 30 '05 #1
7 1167
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
hi stu,

thanks for the prompt help.

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

Jul 30 '05 #3
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

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
Again, thanks Maxim and Alipha!

Jul 30 '05 #6
"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
thanks, it really hepls!

Aug 4 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: joe | last post by:
hi, after reading some articles and faq, i want to clarify myself what's correct(conform to standard) and what's not? or what should be correct but it isn't simply because compilers don't...
3
by: DanielBradley | last post by:
Hello all, I have recently been porting code from Linux to cygwin and came across a problem with static const class members (discussed below). I am seeking to determine whether I am programming...
1
by: yccheok | last post by:
Hi, consider the following bug at #include <stdio.h> class Drink { public: Drink( char *name_ ) : name( name_ ) {} char *name; char & get(int i) { return name; } };
18
by: hzmonte | last post by:
typedef int t_compare_func(const void *, const void *); struct node *tree_search(struct node *root, const void *keyy, t_compare_func *comp) { struct node *cur_item; int result; if (root ==...
3
by: David Scarlett | last post by:
Hi all, I've got a question regarding overriding const member functions with non-const functions. Let's say I've got a base class defined as follows: /*******************/ class Foo {...
15
by: Jiří Paleček | last post by:
Hello, I know the rules for const handling in C++, but I'd like to ask what is the "right" way to use them, eg. when is it appropriate to make a member function const? This came across this...
0
by: d3x0xr | last post by:
Heh, spelled out in black and white even :) Const is useles... do NOT follow the path of considering any data consatant, because in time, you will have references to it that C does not handle,...
2
by: Lionel B | last post by:
I have a function which takes a functor argument. I which to call it for a functor which is actually a class member; this works fine, using the mem_fun_ref and bind1st functions (see listing 1...
2
by: Angus | last post by:
I have a member function, int GetLogLevel() which I thought I should change to int GetLogLevel() const - I made the change and it works fine. But in the function I am creating buffers and of...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.