Archived

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

const And const references

This topic is 6031 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi, I was always wondering what''s the deal with constant variables. What are the advantages of declaring a variable as constant? Is it just a good programming technique or performance issue? ex. void MyFunction (const int Number); Also, whats the deal with constant reference parameters? What''s there advantage? And disadvantage? ex. void Foo (const int &Stuff); Thanks

Share this post


Link to post
Share on other sites
There is more than 1 piece to this issue, but the primary purpose behind the const keyword is to aid in the ability to develop correct programs.

The first way is that a program tells the compiler that a variable "shouldn''t" be changes by giving it the const keyword. Then if somewhere else in the code, somehow someone tries to modify it, the compiler will give you a compile time error, so you can look at the variable and the user and see who made the mistake. This is exactly the same as when compilers started type checking varaibles ... before they did this, if programmer would call a function with incorrect parameter types, the stack would blow up at run time ... and you wouldn''t know how or why .. now it tells you that it can''t match your call to a valid function.

The second use ... a const member function ... serves to tell the user that this function will not modify the object ... and the compiler can enroce these rules and point out anywhere the programmer tries to break them.

The third use ... the const reference ... is a dual purpose beast. The reference was created so that a function could use the syntax of call-by-value(non-pointers in C), with the performance of call-by reference (pointers in C), BUT without the const declaration then a reference parameter can be modified ... which would be scary indead if you call someone''s third party function .. and you don''t know if it modifies your value the value you pass or not. So const to the rescue.

There is an important thing to note: you must use const EVERYWHERE you should if you want to use it at all.

This is because ... say you declare a variable const, but don''t use const memeber functions ... then you can''t call ANY of the functions of that variable. Or if you pass a value by const reference ... without const member functions, once again your stuck.

So ... the modus operandi is this: anything that can be const, should be const, until you learn otherwise. Then make things non-const when you need to for correct operation.

Obviously some things are NEVER const: constructors, destructors, assignment functions, anything that is SUPPOSED to modify an object, and variable that is SUPPOSED to change, etc.

Also ... you need to know this ... you can write a const and non-const version of the same function ... which each are important for different reasons. Here''s an example ... if you write a container class, you would overload the brace operators like this

  
class MyContainer
{
public:
const MyContainer& operator[](int index) const;
MyContainer& operator[](int index);
}


notice that the const version ALSO returns a const ref, and the non-const version returns a non-const ref. This lets each type of object behave exactly as they should (like the built in array and STL types).

ONE MORE THING ... there is ONE area where const / non-const is tricky business -- the defining of interfaces.

When you are making a base class with virtual functions .. do you make them const or not ...

it depends ...

My policy is this: if noone should ever modify the objects with this function, then it is const ... if it''s most likely use must modify the object it is not const. If neither of these apply then it gets harder. You have to balance two conflicting possibilities - A) that someone might want to write a version that modifies their object and B) that someone might want to call this function on a const object. If only A or B apply ... then you know your answer. If A AND B are true ... place BOTH functions into the interface.

Sorry it''s so hard for beginers ... but 6 months from now it''ll be second nature .... best of luck to you

Share this post


Link to post
Share on other sites
constant variables are actually pretty useful, as they allow for stricter variable access.

Variables can be passed to functions in two ways: by value, or by reference.

In the first case, a new copy of the object is made, which is initialized with the value of the original object. This makes sure that the original variable cannot be changed. For built-in types this copy is ok, since they are small. Having

void foo(const int i)

is pretty much useless, since i will be a copy of the integer passed to the function.

For large objects, however, this copy can be very inefficient, so in that case it is better to pass it by reference. But then you could change the object, since your working on a reference to the original object.

To make sure you cannot change the object via the reference, you can make a const reference of it:

void foo(const Type& rt)

This ensures that the object passed to the function cannot be changed. That also means that the only methods of t that can be called, are methods that do not change the object. These methods are called const methods:

class Type
{
public:
void bar() const; // const says that the method won''t change any data of t
};

You can also have const pointers to objects:

void foo(const Type* pt)

foo can only call const methods of pt.

You can also use const as a replacement for #define constants. This has the advantage typechecking also is used on the constant when compiling.

For example:

const int debug=1;

These are the most common uses of const types. There are a couple more, but they''re either not used very often, or I don''t remember them too well to explain

HTH

Share this post


Link to post
Share on other sites
Using const is good programming practice for things you don''t want to manipulate or never plan on. Also, the compiler can add extra performance for const variables that it can''t do for normal variables.

Share this post


Link to post
Share on other sites
If it''s constant, it isn''t a variable.

Still, they do say that constants aren''t and variables don''t, so who really knows what they are.

And just to add something useful to this discussion, with rare exceptions, it''s best if you declare all your references const and pass pointers to anything you wish to change - when it comes to code maintenance it''s much easier to tell if your object is being changed or not without having to look up the prototype. This is especially true of basic data types, such as integers - never pass these by reference (complex objects are assumed to be passed by reference for performance reasons).

Remember, the person who has to maintain your code might be you

--


Get a stripper on your desktop!

Share this post


Link to post
Share on other sites