Sign in to follow this  
Smit

What else is const used for?

Recommended Posts

Smit    310
I understand constant values but why is the keyword "const" used in function parameters/returns etc.? I always see "const char"?

Share this post


Link to post
Share on other sites
dave    2187
Hi bud,

The use of const can be complicated but here are some examples:

Returns a string that can't be edited by the reciever:

const string& GetAnUneditableString()
{

}

Declares that this function can be called on an object of type SomeClass that is const:

unsigned int SomeClass::GetSomeInteger() const
{

}

You may or maynot have known that as well as a normal iterator that there is in fact a const_iterator as well, the item pointed to by the iterator can't be changed. (i think)

vector< string >::const_iterator myIter;

A completely const pointer:

const std::auto_ptr< MyType > myPointerToType;


It goes deeper than that in places, hope that has helped,

Dave

Share this post


Link to post
Share on other sites
Mocanu Razvan    166
In simple terms, const can be attached not only to data types(and make them constant) but also to functions( at the end of the function declaration - working the same way and making it a safe constant function inside an object if you know it's not supposed to modify any member variables; this also means that it can be accessed only from within a const object of that class).
One uses const to create safer code, and also to simplify reading the code. It's much easier to spot a bug or an error or whatever when you impose early restrictions on the return types, functions or variables in your code.
Also, the compiler will optimize the code when it will see const instead of a normal variable. So you should use const whenever you have the chance.
Hope this gives you some insight on the subject.

Share this post


Link to post
Share on other sites
M2tM    948
Quote:
Original post by Dave
Hi bud,

The use of const can be complicated but here are some examples:

Returns a string that can't be edited by the reciever:

const string& GetAnUneditableString()
{

}

Declares that this function can be called on an object of type SomeClass that is const:

unsigned int SomeClass::GetSomeInteger() const
{

}

You may or maynot have known that as well as a normal iterator that there is in fact a const_iterator as well, the item pointed to by the iterator can't be changed. (i think)

vector< string >::const_iterator myIter;

A completely const pointer:

const std::auto_ptr< MyType > myPointerToType;


It goes deeper than that in places, hope that has helped,

Dave


Very good, but for the sake of completing all the basic ones:

void setString(const string &value);

This basically means what you expect. Pass a reference to a function, but make sure the function doesn't change it. This way you don't have to pass by value to ensure your original string isn't tampered with (of course, unlike pass by value, the function cannot change the passed in parameter.)

There are other cases for const, but basically you use it when you want to ensure the value is not changed.

Share this post


Link to post
Share on other sites
DaBono    1496
And you can go completely ballistic by doing:
char const * const getName() const { return name; }

You can now read the consts from right-to-left.
- The right-most const says getName() won't change the object that it's part of.
- The middle const says you cannot change the pointer that the method returns.
- The left-most const says that you may not change the value the returned pointer points to.

NOTE (OT): Whenever you find yourself typing the above declaration, consider using std::string. [wink]

Share this post


Link to post
Share on other sites
dave    2187
Quote:
Original post by DaBono
And you can go completely ballistic by doing:
char const * const getName() const { return name; }

You can now read the consts from right-to-left.
- The right-most const says getName() won't change the object that it's part of.
- The middle const says you cannot change the pointer that the method returns.
- The left-most const says that you may not change the value the returned pointer points to.


I was going to post that but then i started thinking about food.

Dave

Share this post


Link to post
Share on other sites
simon10k    220
Quote:
Original post by Dave
Declares that this function can be called on an object of type SomeClass that is const:

unsigned int SomeClass::GetSomeInteger() const
{

}


I thought this means that the function can't change the value of the class members.

Share this post


Link to post
Share on other sites
C J W    226
Quote:
Original post by simon10k
Quote:
Original post by Dave
Declares that this function can be called on an object of type SomeClass that is const:

unsigned int SomeClass::GetSomeInteger() const
{

}


I thought this means that the function can't change the value of the class members.


A const method, like the one suggested above can not alter the object it is called upon. There are exceptions to this rule, the method could have some members declared as mutable, which will allow them to be changed inside a const method.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster   
Guest Anonymous Poster
const char * const MyConst::getConst(const char * const c) const { static const sc = 0; return ≻ }

All const and no mutable makes AP go const! Or... something like that...

Share this post


Link to post
Share on other sites
Way Walker    745
Quote:
Original post by simon10k
Quote:
Original post by Dave
Declares that this function can be called on an object of type SomeClass that is const:

unsigned int SomeClass::GetSomeInteger() const
{

}


I thought this means that the function can't change the value of the class members.


Six of one, half dozen of the other.

If you can call the function on a const object, then it can't change the value of (non-mutable) class members.

If the function can't change the value of (non-mutable) class members, then you can call it on a const object.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this