Archived

This topic is now archived and is closed to further replies.

TheBlackJester

const after a function?

Recommended Posts

Hey, I''m getting ready to write a math header for vectors, matrices, etc, and I''ve noticed one thing. I was looking through OpenGL Game Programming to get an idea of how its done, and I noticed that a few of the operators have the ''const'' keyword after them, as well as before. I''m just wondering what purpose it serves in that context. Here''s an example.
// Vector subtraction
const CVector operator-(const CVector& vec) const
 
Thanx ahead of time


"With my feet upon the ground I lose myself between the sounds and open wide to suck it in, I feel it move across my skin. I''m reaching up and reaching out. I''m reaching for the random or what ever will bewilder me, what ever will bewilder me. And following our will and wind we may just go where no one''s been. We''ll ride the spiral to the end and may just go where no one''s been." - Maynard James Keenan
[TheBlackJester ]
[Wildfire Studios ]

Share this post


Link to post
Share on other sites
The const after the function promises the compiler that this function won''t be changing any of its data members.

Hope this helps!

-G|aD-

Share this post


Link to post
Share on other sites
const before a function means the RETURN type is const, const before a parameter means that parameter is const ... and const after the function means the function is const. This can only have any meaning for member functions ... because normal functions and static functions don''t have any member variables to promise not to change.

Share this post


Link to post
Share on other sites
ok so what about

int *var const;
and
int const *var

Does one of them mean the address of another variable is const in the pointer variable? And does the other one mean the value where the address points to is const??

[edited by - eXistenZ on September 2, 2002 1:25:50 AM]

Share this post


Link to post
Share on other sites
int *x const; is nothing I believe. Does it even compile?

The classic rule is to read it backwards to understand it.

int * const x; // X is a const pointer to an int. The point can not change.

const int* x; // X is a pointer to a const int. The pointer can only point to a const int.

Const helps compiler optimization in many cases. Another rule many people go by (as a semi-joke) is to simply make everything const, then remove them until it works.

Share this post


Link to post
Share on other sites
as Taulin said ...

int *x const
// is invalid c++

int * const x;
// x is a constant pointer to an int. The pointer cannot change.

const int* x;
// x is a pointer to a const int. The pointer can only point to a const int.

and also ...

const int * const x;
// x is a constant pointer to a constant int ...

this syntax:

const TYPE * VARAIBLE;

is very common, to provide pointer to variable you cannot change ...

this syntax:

TYPE * const VARIABLE;

is really not very important, because we have references which server the same purpose ... you can replace these:

int * const x;

with these:

int &x;

of course you have to change -> to . and make sure it doesn''t need to be null ...

Share this post


Link to post
Share on other sites