Quote:Original post by xissburg
is there any performace difference between then?!?
Try it out in your favourite compiler!!!
Quote:Original post by xissburg
is there any performace difference between then?!?
Quote:Original post by NamethatnobodyelsetookNo, it can't. Because there's const_cast that takes away constness. And if the compiler is smart enough to see if you're using const_cast, then the compiler damn sure is smart enough to see if you're modifying the values. Know that const objects can also be passed to other functions which may use const_cast on them to cast away constness. So using "const" buys you nothing in terms of speed.
const can make your app faster... If you were to access a member of a struct, call a function that took that struct as const, then access the struct again. When the function takes const parameters, or is a const member function, the compiler knows the struct cannot change during the function call. This allows the compiler to possibly optimize the second access to the structure, by just using the value read the first time.
Quote:Original post by Namethatnobodyelsetook
const can make your app faster... If you were to access a member of a struct, call a function that took that struct as const, then access the struct again. When the function takes const parameters, or is a const member function, the compiler knows the struct cannot change during the function call. This allows the compiler to possibly optimize the second access to the structure, by just using the value read the first time.
Quote:Original post by Anonymous PosterQuote:Original post by NamethatnobodyelsetookNo, it can't. Because there's const_cast that takes away constness. And if the compiler is smart enough to see if you're using const_cast, then the compiler damn sure is smart enough to see if you're modifying the values. Know that const objects can also be passed to other functions which may use const_cast on them to cast away constness. So using "const" buys you nothing in terms of speed.
const can make your app faster... If you were to access a member of a struct, call a function that took that struct as const, then access the struct again. When the function takes const parameters, or is a const member function, the compiler knows the struct cannot change during the function call. This allows the compiler to possibly optimize the second access to the structure, by just using the value read the first time.
VECTOR3 vec(1,1,1);void f(const VECTOR& v){ vec.x = vec.y = vec.z = 0;}void g(const VECTOR& v){ // v = (1,1,1) f(v); // v = (0,0,0)}int main(){ g(vec);}
Quote:Original post by SamLowryI also mentioned this.Quote:Original post by Anonymous Poster
No, it can't. Because there's const_cast that takes away constness. And if the compiler is smart enough to see if you're using const_cast, then the compiler damn sure is smart enough to see if you're modifying the values. Know that const objects can also be passed to other functions which may use const_cast on them to cast away constness. So using "const" buys you nothing in terms of speed.
Incorrect.
You're right about const not making any difference in performance, but the reason is that a compiler can easily find out by itself whether or not a function modifies its arguments or not (in the case of the dot product function), so even if you do not specify an argument as const, the compiler may still perform optimisations as if it were, as long as the argument isn't modified in any way.
Quote:const_cast is an entirely different affair. It is an error to const_cast a const value and then modify it.I was thinking of pointers and references to const objects, which can be legally const_casted and modified.
Quote:Original Post by Eddycharly
adding const prevents the function to modify the parameters passed to it, even if they are passed by adress or by reference.
Quote:Original post by xissburgConstant correctness is an important concept, and requires using the keyword consistently even when it doesn't seem that important. Here are some links that will explain it better than I can (just hits from google):
but the Vec3Dot() function for example doesnt change the VECTOR3s passed as arguments!! so, why use it? is there a chance of occur an error and the VECTORs get chaged?!? dont understand why to use const sometimes...