Accelerated C++
const
Modifies built-in type variables, custom objects, member functions, return values, and function parameters to ensure that a certain value remains unchanged.
<span>const</span> modifies a variable, acting as a constant that can be assigned to a new variable, but the constant itself cannot change.
const int a=7;
int b = a; // valid
a = b // invalid
<span>const</span> modifies pointer variables.
- Modifies the content pointed to by the pointer, making the content an immutable variable.
const int *p = 8;
The content corresponding to the address pointed to by the pointer is immutable, meaning 8 is unchangeable.
- Modifies the pointer itself, making the pointer an immutable content.
int a = 8,b=9;
int* const p = &a;
*p = 9;// valid
p = &b;// invalid
The address pointed to by the pointer is immutable, but the corresponding content can change.
- Modifies both the pointer and the content it points to, making both the pointer and the content immutable, effectively combining
<span>1</span>and<span>2</span>.
int a = 8;
const int *const p = &a;
Thus, both the address and the content at that address cannot be modified.
<span>const</span> combined with <span>&</span> avoids value copying, improving efficiency.
- Avoids the overhead of copying when passing containers (reference passing).
- Ensures that the content will not be modified (
<span>const</span>restriction).
void printScores(const vector<double>& hw) {
// Can read elements in hw, but cannot modify
for (double score : hw) {
cout << score << " ";
}
}
Exception Handling
<span>throw</span>: When a problem occurs, the program throws an exception, completed by<span>throw</span>.<span>catch</span>: At the point where the problem needs to be handled, the exception handler captures the exception, completed by<span>catch</span>.<span>try{}</span>: Contains code that may throw an exception.
Throwing exception code block
double division(int a, int b){
if( b == 0 ) {
throw "Division by zero condition!";
}
return (a/b);
}
Catching exception code block
try{
c = division(1,0);
}catch (const char* msg){
cout << msg << endl;
}
At this point, the parameter for b is 0, which will trigger the exception to be thrown. The main function calls and captures it, then it will enter the
<span>catch</span>to handle the exception, rather than continue executing the code in<span>try{}</span>.
This is a function to find the median.
double median(vector<double> homework) {
typedef vector<double>::size_type vector_size;
vector_size homework_size = homework.size();
if (homework_size == 0) {
throw domain_error("median of an empty vector");
}
sort(homework.begin(), homework.end());
vector_size mid = homework_size / 2;
return homework_size % 2 == 1 ? homework[mid] : (homework[mid] + homework[mid - 1]) / 2;
}
The throwing exception part is when the vector length is 0, an exception is thrown.
if (homework_size == 0) {
throw domain_error("median of an empty vector");
}
<span>domain_error</span>is an exception object that indicates “the parameter’s value range does not meet the function’s requirements”, which is a definition domain “error”.
Exception capturing and handling
try {
// omitted here
}catch (const domain_error& e){
cout << e.what();
}
<span>.what()</span>is used to return a string describing the exception information, returning a pointer of type<span>const char*</span><span>.</span>