Okay lets keep this short and sweet....
while doing custom sorting using the sort function present in <algorithm> I saw a construct (or rather a way of programming) which i rarely (read never) use. This was this:
//normal part
typdef struct
{
}CoolStruct;
bool mySort(CoolStruct const &lhs, CoolStruct const &rhs )
{...}
Now why are we actually using const &varName....???
So i stumbled upon this article
http://duramecho.com/ComputerInformation/WhyHowCppConst.html
Now the thing related to using const while parameter passing is this:
passing structure would incur making copy of it. Now this might take a lot of time depending upon the size of the structure. One way to bypass this is passing a refernce to it using ( & symbol) in C++. But then you can make changes to it.... so while working on libraries if you want to make sure that nobody alters the passed refernce and that you are doing it for optimization purposes only "const" is added. So its saying that pass a refence so that no copying is involved ... and i want to protect the data too so don't alter it.
Further the article is very nicely written and worth a read.
Enjoy ...future Mukul while reading it.
without wax
Mukul on 23 may 2014
while doing custom sorting using the sort function present in <algorithm> I saw a construct (or rather a way of programming) which i rarely (read never) use. This was this:
//normal part
typdef struct
{
}CoolStruct;
bool mySort(CoolStruct const &lhs, CoolStruct const &rhs )
{...}
Now why are we actually using const &varName....???
So i stumbled upon this article
http://duramecho.com/ComputerInformation/WhyHowCppConst.html
Now the thing related to using const while parameter passing is this:
passing structure would incur making copy of it. Now this might take a lot of time depending upon the size of the structure. One way to bypass this is passing a refernce to it using ( & symbol) in C++. But then you can make changes to it.... so while working on libraries if you want to make sure that nobody alters the passed refernce and that you are doing it for optimization purposes only "const" is added. So its saying that pass a refence so that no copying is involved ... and i want to protect the data too so don't alter it.
Further the article is very nicely written and worth a read.
Enjoy ...future Mukul while reading it.
without wax
Mukul on 23 may 2014
No comments:
Post a Comment