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

const functions

P: n/a
Pmb
What does it meant when a function member of a class is declared as const?

Thanks

Pmb
Jul 22 '05 #1
Share this Question
Share on Google+
15 Replies


P: n/a

"Pmb" <so*****@somewhere.com> wrote in message
news:X5********************@comcast.com...
What does it meant when a function member of a class is declared as const?


That it does not modify the object on which it is called.

john
Jul 22 '05 #2

P: n/a

"Pmb" <so*****@somewhere.com> wrote in message
news:X5********************@comcast.com...
What does it meant when a function member of a class is declared as const?


Always check the FAQ first.
http://www.parashift.com/c++-faq-lit...html#faq-18.10
Jul 22 '05 #3

P: n/a
Pmb wrote:

What does it meant when a function member of a class is declared as const?


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.
--
Karl Heinz Buchegger
kb******@gascad.at
Jul 22 '05 #4

P: n/a
Pmb

"Sharad Kala" <no*****************@yahoo.com> wrote in message
news:2h************@uni-berlin.de...

"Pmb" <so*****@somewhere.com> wrote in message
news:X5********************@comcast.com...
What does it meant when a function member of a class is declared as
const?
Always check the FAQ first.
http://www.parashift.com/c++-faq-lit...html#faq-18.10


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"

Thanks

Pmb
Jul 22 '05 #5

P: n/a
Pmb wrote:

"Sharad Kala" <no*****************@yahoo.com> wrote in message
news:2h************@uni-berlin.de...

"Pmb" <so*****@somewhere.com> wrote in message
news:X5********************@comcast.com...
What does it meant when a function member of a class is declared as

const?

Always check the FAQ first.
http://www.parashift.com/c++-faq-lit...html#faq-18.10


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"


Exactly what it says.

There is a class.

There is an object of this class.

Now this object is used by some client code.

The client code calls a member function of this object.

When calling the member function, the client code will not
notify any changes in the state of that object.

Example:
I write a class which models a person. The state of that person
consists of its name and its birthdate.
I add a member function to that class which allows you to get
the birthdate. But by calling that function, the object will
not change it's visible state: Neither will the name change
nor will the birthdate change. Thus I will make that function
a const one.

--
Karl Heinz Buchegger
kb******@gascad.at
Jul 22 '05 #6

P: n/a
Pmb

"Karl Heinz Buchegger" <kb******@gascad.at> wrote in message
news:40***************@gascad.at...
Pmb wrote:

What does it meant when a function member of a class is declared as const?


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.


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.

Perhaps I don't understand what is meant above by the "state of the object"?

Thanks

Pmb

--------------------------------------------------------------------------
#include <iostream.h>

class Test{
public:
Test( int = 0, int = 0 );
void print( int , int ) const;
private:
int x;
int y;
};

Test::Test( int i , int j )
{
x = i;
y = j;
cout << "Object Test constructed with x = " << x << ", y = " << y <<
endl;
}

void Test::print ( int x, int y ) const
{
cout << "x in print() is " << x << endl;
cout << "y in print() is " << y << endl;
}

int main()
{
Test const test( 1, 2 );

test.print ( 8, 9 );

return 0;

}
---------------------------------------------------------------------------
Jul 22 '05 #7

P: n/a

"Pmb" <so*****@somewhere.com> wrote in message
news:m7********************@comcast.com...

"Karl Heinz Buchegger" <kb******@gascad.at> wrote in message
news:40***************@gascad.at...
Pmb wrote:

What does it meant when a function member of a class is declared as const?
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.


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.


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.
Does that mean that I've changed the value of the object or
the state of the object.
No you haven't.

Perhaps I don't understand what is meant above by the "state of the

object"?

I think you do, the problem it that you don't see that you have two
different x's and two different y's in your program.

john
Jul 22 '05 #8

P: n/a
Pmb wrote:

"Karl Heinz Buchegger" <kb******@gascad.at> wrote in message
news:40***************@gascad.at...
Pmb wrote:

What does it meant when a function member of a class is declared as const?


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.


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.


Where?
You didn't

In the print function you printed the values passed
to that function, not the member variables.

--
Karl Heinz Buchegger
kb******@gascad.at
Jul 22 '05 #9

P: n/a

"Pmb" <so*****@somewhere.com> wrote in message
news:JN********************@comcast.com...
[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"


Say you have a class that contains a char pointer as a member. You allocate
memory for that in the constructor. Later in a member function you only change
the contents of the allocated memory . Now as far as client is concerned there
is constness (no change in value of p though what it points to has changed),
hence it can be declared const.

-Sharad
Jul 22 '05 #10

P: n/a
Pmb

"John Harrison" <jo*************@hotmail.com> wrote in message
news:2h************@uni-berlin.de...

"Pmb" <so*****@somewhere.com> wrote in message
news:m7********************@comcast.com...

"Karl Heinz Buchegger" <kb******@gascad.at> wrote in message
news:40***************@gascad.at...
Pmb wrote:
>
> What does it meant when a function member of a class is declared as const?
>

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.


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.


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.


Oops! Thanks

Pmb
Jul 22 '05 #11

P: n/a
Looking at your code bellow

int main()
{
Test const test( 1, 2 );
You made the values of x and y equal to 1 and 2.
But not any x and y, the x and y that belongs to the class Test.
To do that you used to values i and j

test.print ( 8, 9 );
now your function prints out two values that you have passed 8 and 9 but to
pass the values you called them x and y in your function.
The fact that the class Test also has two place holders called x and y is
irrelevant really, (but bad programming).
If you change your function to
void 'Test::print ( int i, int j )' const the output will be the value of
the x and y in the class Test, (and i and j would effectively be ignored).

return 0;

}
-------------------------------------------------------------------------- -


As for a const function it is a function that does not assign values within
it's body.
if you did

void Test::print ( int i, int j ) const
{
x = j;

cout << "x in print() is " << x << endl;
cout << "y in print() is " << y << endl;
}

it would not work because you are trying to change the value of x. That is
not permitted in a const function.

(note that i oversimplified what happens so you get a better idea of what is
going on).
Simon.
Jul 22 '05 #12

P: n/a
Pmb posted:
What does it meant when a function member of a class is declared as
const?

Thanks

Pmb

int DoStuff(const Dog& doggie)
{
doggie.age = 5;

//ERROR, CANNOT EDIT const OBJECT

return 0;

}
-JKop
Jul 22 '05 #13

P: n/a

"JKop" <NU**@NULL.NULL> wrote in message
news:Nu***************@news.indigo.ie...
Pmb posted:
What does it meant when a function member of a class is declared as
const?
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.
Jul 22 '05 #14

P: n/a
Sharad Kala posted:
His question is that what is a const member function if you read
carefully.

Opps!!!

int Dog::DoStuff(void) const
{
age = 5;

//ERROR, CANNOT EDIT OBJECT, THIS IS A const FUNCTION!!

return 0;
}

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 .
-JKop
Jul 22 '05 #15

P: n/a
JKop wrote:
...
Checklist:

1) Does your function edit object member variables? If not, declare it
const .
Not correct. For example, take a look at non-const version of
'std::vector::operator[]'. It doesn't edit any of the object's member
variables. Do you think it should've been declared as 'const'? What
about non-const versions of 'begin()' and 'end()' methods in standard
containers?
3) Does your function neither read nor edit member variables? If so, declare
it static .


Not exactly correct either (for similar reasons).

BTW, where is 2) ?

--
Best regards,
Andrey Tarasevich

Jul 22 '05 #16

This discussion thread is closed

Replies have been disabled for this discussion.