Archived

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

Jx

Uses of "const"

Recommended Posts

I''m just getting to grips properly with c++ and I am a little curious about the uses of the "const" keyword. I would like to know what it means in the following situations: void foo(const char& var) and int bar(char *var) const Can anyone help? Thanks in advance Jx

Share this post


Link to post
Share on other sites
You can think of it as making the data pointer points at read-only (the character pointer (ok, reference, same diff) in the first case, "this" in the second).

This helps the optimizer out some, as it can assume data will not be changed by the function call. It can also help prevent silly bugs or unexpected side-effects.

Share this post


Link to post
Share on other sites
So the second useage should be used for "get" methods?

i.e.

int getInt( ) const
{
...
}

because we should not modify anything in there?

Share this post


Link to post
Share on other sites
Const in many ways is also simply to indicate to the programmer "Don't expect your value to change in here." Same if you pass a pointer... typically that means "This will change." It's good programming practice to use const whenever you do not plan to change a parameter... that way, everyone is crystal clear.

Here's from MSDN:

    
Constant Member Functions
C++ Specific

Declaring a member function with the const keyword specifies that
the function is a "read-only" function that does not modify the object for which it is called.

To declare a constant member function, place the const keyword after the closing parenthesis of the argument list.
The const keyword is required in both the declaration and the definition.
A constant member function cannot modify any data members or call any member functions that aren't constant.

END C++ Specific

Example

// Example of a constant member function

class Date
{
public:
Date( int mn, int dy, int yr );
int getMonth() const; // A read-only function

void setMonth( int mn ); // A write function;

// cannot be const

private:
int month;
};

int Date::getMonth() const
{
return month; // Doesn't modify anything

}
void Date::setMonth( int mn )
{
month = mn; // Modifies data member

}


"You call him Dr. Grip, doll!"

Edited by - the_grip on August 15, 2001 9:55:46 AM

Share this post


Link to post
Share on other sites
quote:
Original post by the_grip
Const in many ways is also simply to indicate to the programmer "Don''t expect your value to change in here."



Don''t know about you, but my compiler enforces constness to be applied to anything declared const.

You declare references const whenever possible, since you usually pass by reference for effeciency''s sake - if you want to change the value, you can use a reference or a pointer, but bear in mind that if you use a reference your maintenance programmer will hate you (It''s easy to see from context when things are passed by pointer. Otherwise you need the prototype on hand).



--


Games, Anime and more at GKWorld

Meet Bunny luv'' and the girls, ready to perform on your desktop just for you (warning: adult content).

Share this post


Link to post
Share on other sites