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

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


Oops! Thanks

Pmb


 
Reply With Quote
 
 
 
 
Simon
Guest
Posts: n/a
 
      05-24-2004
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:rint ( 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:rint ( 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.


 
Reply With Quote
 
 
 
 
JKop
Guest
Posts: n/a
 
      05-24-2004
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
 
Reply With Quote
 
Sharad Kala
Guest
Posts: n/a
 
      05-24-2004

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


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

> His question is that what is a const member function if you read
> carefully.



Opps!!!



int Dog:oStuff(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
 
Reply With Quote
 
Andrey Tarasevich
Guest
Posts: n/a
 
      05-24-2004
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: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) ?

--
Best regards,
Andrey Tarasevich

 
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