cplusplus
.com
TUTORIALS
REFERENCE
ARTICLES
FORUM
C++
Tutorials
Reference
Articles
Forum
Forum
Beginners
Windows Programming
UNIX/Linux Programming
General C++ Programming
Lounge
Jobs
Forum
Beginners
Is the "const" specifier necessary? Any
Is the "const" specifier necessary? Any benefit?
Mar 8, 2012 at 5:33am UTC
foxatlarge
(7)
#include <iostream>
using namespace std;
void printBinary(const unsigned char val) {
for(int i = 7; i >= 0; i--)
if(val&(1 << i))
cout << "1";
else
cout << "0";
}
I tried calling the above function without "const" specifier, and it still works.
Any potential risk without the "const"?
Mar 8, 2012 at 6:08am UTC
Disch
(13742)
Not really. In this case all it means it that val cannot be changed in the printBinary function. It might allow for some minor compiler optimizations, but it likely won't make any difference.
Mar 9, 2012 at 2:45am UTC
foxatlarge
(7)
Thanks for your elaboration, which is helpful. I appreciate it.
Topic archived. No new replies allowed.