'const' keyword can be esential to give speed ?

I have read an explanation about the 'const' keyword used at methods and functions.

Passing data to methods by reference are faster, and if I use const are event faster ?[1]
And using pointers are even faster ? [2]
And if I use const at the end of body of my method (void my_method(int value) const ),means I dont want to change any member variables of my class.... This way is another statregy to give power to code ? [3]

Can anybody answer this 3 questions. Pros & cons ?
Thanks
1. Not necessarily faster, but const has different semantics. It tells me that the function will not modify the parameter, and knowing this could be very important. In fact, if you do not use const, I will assume that the function will modify the parameter.

2. Pointers faster than const?! You are comparing apples and oranges.

3. Doing so allows the compiler to optimize. Look here:
http://stackoverflow.com/questions/212237/constants-and-compiler-optimization-in-c
Thanks kfmfe04.
Regarding 2:
I can use
1 my_function(value)
2 my_function(const &value)
3 my_function(const * value)
In this third case, I use const and pointer (this was my question...)
Are 3 faster than 2 and 1 ?
You are thinking about it the wrong way - in your specific case, do not think about parameter passing in terms of efficiency (although, you can infer something from my comments below).

Instead, think of it in terms of semantics (meaning):

1. Tells me that value is cheap to copy and I only want to pass it in for input. It's good to use this form for primitive types: int, double, char, bool, etc...

2. Tells me that the parameter must be non-NULL (because it's a reference) and that it can't be modified. No copy-construction is done. It's good to use this form for objects that cannot be NULL.

3. Tells me that the parameter might be NULL (I should check) and what is pointed to can't be modified. Use this form when pointing to nothing has meaning in your program.

There are some borderline small objects which may be passed different ways. For example std::string is often passed using 1. (because it's cheap to copy, or just due to ignorance), but I actually prefer 2. because it's more efficient.

However, there are situations where I may use 1. over 2. For example, if I want a function to be thread-safe, I would prefer 1, rather than add a mutex to the passed in parameter. It certainly depends on the situation.
Last edited on
Hello tonnot,

using 'const' doesn't make your programm always faster. it might help the compiler optimizing the code so it might be faster.

[1] for complex data it is faster. for primitives like 'int' most likely not.
[2] pointers are not faster than reference (in most cases they're the same)
[3] 'const' helps to make your program more fail safe
(Note that a threadsafe STL would make std::string copying expensive, because the current COW semantics
will have to be changed for std::string. Therefore, you should get into the habit of passing std::strings by
const or non-const reference).
Topic archived. No new replies allowed.