Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > const functions

Reply
Thread Tools

const functions

 
 
Pmb
Guest
Posts: n/a
 
      05-24-2004
What does it meant when a function member of a class is declared as const?

Thanks

Pmb


 
Reply With Quote
 
 
 
 
Sharad Kala
Guest
Posts: n/a
 
      05-24-2004

"Pmb" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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


 
Reply With Quote
 
 
 
 
John Harrison
Guest
Posts: n/a
 
      05-24-2004

"Pmb" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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


 
Reply With Quote
 
Karl Heinz Buchegger
Guest
Posts: n/a
 
      05-24-2004
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
http://www.velocityreviews.com/forums/(E-Mail Removed)
 
Reply With Quote
 
Pmb
Guest
Posts: n/a
 
      05-24-2004

"Sharad Kala" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
>
> "Pmb" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
> > 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


 
Reply With Quote
 
Karl Heinz Buchegger
Guest
Posts: n/a
 
      05-24-2004
Pmb wrote:
>
> "Sharad Kala" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
> >
> > "Pmb" <(E-Mail Removed)> wrote in message
> > news:(E-Mail Removed)...
> > > 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
(E-Mail Removed)
 
Reply With Quote
 
Pmb
Guest
Posts: n/a
 
      05-24-2004

"Karl Heinz Buchegger" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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:rint ( 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;

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


 
Reply With Quote
 
Karl Heinz Buchegger
Guest
Posts: n/a
 
      05-24-2004
Pmb wrote:
>
> "Karl Heinz Buchegger" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
> > 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
(E-Mail Removed)
 
Reply With Quote
 
John Harrison
Guest
Posts: n/a
 
      05-24-2004

"Pmb" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
>
> "Karl Heinz Buchegger" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
> > 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


 
Reply With Quote
 
Sharad Kala
Guest
Posts: n/a
 
      05-24-2004

"Pmb" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
>

[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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
const vector<const MyType> Vs const vector<MyType> magnus.moraberg@gmail.com C++ 2 02-09-2009 10:45 PM
is const necessary in eg int compar(const void *, const void *) lovecreatesbeauty@gmail.c0m C Programming 26 11-10-2008 09:47 PM
const correctness - should C++ prefer const member over non-const? fungus C++ 13 10-31-2008 05:33 AM
const vector<A> vs vector<const A> vs const vector<const A> Javier C++ 2 09-04-2007 08:46 PM
Casting int'** to 'const int * const * const' dosn't work, why? Jonas.Holmsten@gmail.com C Programming 11 07-01-2007 06:16 PM



Advertisments