Value Categories and References
Const Reference Params
Const references let a function read an existing object without copying or modifying it.
Const Reference Params
const_reference_params.cpp
#include <iostream>
#include <string>
int label_size(const std::string& label) {
return static_cast<int>(label.size());
}
int main() {
std::string label = ;
int size = label_size(label);
std::cout << "label=" << label << std::endl;
std::cout << "size=" << size << std::endl;
return 0;
}
#include <iostream>
#include <string>
int label_size(const std::string& label) {
return static_cast<int>(label.size());
}
int main() {
std::string label = ;
int size = label_size(label);
std::cout << "label=" << label << std::endl;
std::cout << "size=" << size << std::endl;
return 0;
}
#include <iostream>
#include <string>
int label_size(const std::string& label) {
return static_cast<int>(label.size());
}
int main() {
std::string label = ;
int size = label_size(label);
std::cout << "label=" << label << std::endl;
std::cout << "size=" << size << std::endl;
return 0;
}
const reference
A const reference parameter can bind to an existing object while promising not to change it.
read-only parameter
Read-only parameters are useful for strings and containers that should not be copied just to inspect them.