Original Post
Should i use "const type &some_var" every time I am passing an argument wich isn't gonna be modified later inside the function?... and why? Thanks in advance.
Quote:
Original post by uavfun
You may mark them with const but this is of relatively limited use - it won't have an effect on code that calls the function; it only stops you from modifying the parameter inside the function (which has no effect on whatever was originally passed to it).
Quote:
Original post by kaysik
Except if you have a const variable you want to pass into the function!
void foo(int a){ cout << a << endl;}void bar(){ const int c = 45; foo(c);}/* this usage prevents an argument from being modified within the function. */void foo(const int);/* this usage prevents a function from modifying other members of the class. */class bar {private: // (Add some member variables here)public: void foo(int) const;}/* similar to the first example, this usage prevents the return value of the function from being modifed. */const int func(); void foo(int& a) Quote:
Original post by Spudder
Herb Sutter has two atricles on his Guru Of The Week site, one on const correctness which gives guidelines on how and when to use const and another on how effective const is in producing optimised code. Both are well worth a read to get an idea of proper const-correctness.
Quote:*Ahem* Literals. The only way to support both the efficient passing of arbitrarily-sized objects and literal parameters is via constant reference.
Original post by IndigoDarkwolf Quote:Well, uavfun was referring to native types, such as int, unsigned int, char, double, float, etc. These types are so small that there is usually no (or almost no) penalty for passing them by-value.
Original post by kaysik Quote:Except if you have a const variable you want to pass into the function!
Original post by uavfun
You may mark them with const but this is of relatively limited use - it won't have an effect on code that calls the function; it only stops you from modifying the parameter inside the function (which has no effect on whatever was originally passed to it).
void SomeFunction(std::string s);void SomeOtherFunction(const & std::string s);...std::string str;...SomeFunction(str); // OkaySomeFunction("Some String"); // Blows up, or necessitates construction of temporary object and copyingSomeOtherFunction(str); // OkaySomeOtherFunction("Some String"); // Okay
Quote:
Original post by glaskows
Thanks for the replies.
About the article const correctness I didn't quite understand why the author declared "CalcArea()" as a const member function when, in fact, it does change the member variable "area_".
GotW is going right to my bookmarks :)
Quote:
Original post by glaskows
Thanks for the replies.
About the article const correctness I didn't quite understand why the author declared "CalcArea()" as a const member function when, in fact, it does change the member variable "area_".
GotW is going right to my bookmarks :)
This topic has been locked by a moderator. New replies are not allowed.
With your permission, GameDev.net uses analytics cookies to understand how people use the platform. You can accept analytics or continue with necessary cookies only. Learn more