const functions

K

Karl Heinz Buchegger

Pmb said:
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.
 
K

Karl Heinz Buchegger

Pmb said:
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.
 
P

Pmb

Karl Heinz Buchegger said:
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;

}
---------------------------------------------------------------------------
 
K

Karl Heinz Buchegger

Pmb said:
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.
 
J

John Harrison

Pmb said:
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
 
S

Sharad Kala

Pmb said:
[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
 
P

Pmb

John Harrison said:
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
 
S

Simon

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.
 
J

JKop

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
 
J

JKop

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
 
A

Andrey Tarasevich

JKop said:
...
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::eek:perator[]'. 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) ?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top