I have for example this:
void SetWidth(double width);
Should it be:
void SetWidth(const double width);
I have for example this:
void SetWidth(double width);
Should it be:
void SetWidth(const double width);
No, you shouldn't.
The compiler treats the two function signatures as identical, so about all you can accomplish by adding const
in a case like this is mislead the reader. For one obvious example, a reader who doesn't know C++ quite so well might believe that having both:
void SetWidth(double) {}
void SetWdith(const double) {}
...would be legitimate function overloading. This, of course, is wrong -- as already noted, the compiler treats the two as identical, so this would simply violate the one definition rule.
The const
does prevent you from modifying the parameter inside the function, but if your function is large enough that you think you need this (i.e., you're having difficulty keeping track of what's being modified and what isn't), then chances are pretty good that you need to rewrite the function.
From the caller's viewpoint, both are equivalent. You can (and, to reduce noise, should) leave that const
out of a declaration, and only include it in the definition.
From the implementor's viewpoint, you might want to do it for the same reason you might want any local variable to be constant: to be sure that it won't change by accident.
Short answer: implement a function that takes a const
argument by value when it makes sense to ensure (or explicitly express) that: "This argument will not change inside the body of this function."
and implement a function that will take const
argument by reference, when you want to explicitly tell the caller: "This function will not change the argument you're passing to it."
(In case of the "setter" taking an argument by value - such as SetWidth
- it doesn't make much of a sense to use const
)
They are almost equivalent.
The only difference is that you tell to the compiler that the variable width
cannot be modified inside the function scope when you use const
.
For, Void SetWidth(double width);
We can change the value of the variable width with in the function SetWidth(). But, the changed value will not reflect back to the called function. Say,
class A_Class
{
public:
void SetWidth(double width) {
width=width+10;
this._width=width;
std::cout<<"IN SetWidth:"<<width;
}
private:
double _width;
};
int main()
{
A_Class aObj;
double twidth=20.9;
aObj.SetWidth(twidth);
std::cout<<"twidth:"<<twidth;
return 0;
}
O/P: IN SetWidth:30.9
twidth:20.9
So, it doesn't matter if make it as const, which you have mentioned in the other proto-type [void SetWidth(const double width);].
Internally compiler allocates memory for 'width' variable in the stack and copies the value to that variable. If you use the second proto-type[void SetWidth(const double width);] it ensures that variable width is constant can not be changed inside the function.
Hope this helps.
If the function is not expected to modify the local variable width
, you should declare it const
. This way, if you inadvertently try to assign to the variable, or assign its address to a non-const pointer (since that could allow it to be modified), the compiler will issue a warning.
There's no difference in the generated code in either case, it just enables these additional safety checks.