Online Book Reader

Home Category

Professional C__ - Marc Gregoire [15]

By Root 1529 0
in a denominator of zero.

#include

double divideNumbers(double inNumerator, double inDenominator)

{

if (inDenominator == 0) {

throw std::exception();

}

return inNumerator / inDenominator;

}

When the throw line is executed, the function will immediately end without returning a value. If the caller surrounds the function call with a try/catch block, as shown in the following code, it will receive the exception and be able to handle it.

#include

#include

int main()

{

try {

std::cout << divideNumbers(2.5, 0.5) << std::endl;

std::cout << divideNumbers(2.3, 0) << std::endl;

} catch (const std::exception& exception) {

std::cout << "An exception was caught!" << std::endl;

}

return 0;

}

The first call to divideNumbers() executes successfully, and the result is output to the user. The second call throws an exception. No value is returned, and the only output is the error message that is printed when the exception is caught. The output for the preceding block of code is:

5

An exception was caught!

Exceptions can get tricky in C++. To use exceptions properly, you need to understand what happens to the stack variables when an exception is thrown, and you have to be careful to properly catch and handle the necessary exceptions. The preceding example used the built-in std::exception exception type, but it is preferable to write your own exception types that are more specific to the error being thrown. Unlike the Java language, the C++ compiler doesn’t force you to catch every exception that might occur. If your code never catches any exceptions but an exception is thrown, it will be caught by the program itself, which will be terminated. These trickier aspects of exceptions are covered in much more detail in Chapter 10.

The Many Uses of const

The keyword const can be used in several different ways in C++. All of its uses are related, but there are subtle differences. One of the authors has discovered that the subtleties of const make for excellent interview questions! In Chapter 9, you will learn all of the ways that const can be used. The following sections outline the most frequent uses.

const Constants

If you assumed that the keyword const has something to do with constants, you have correctly uncovered one of its uses. In the C language, programmers often use the preprocessor #define mechanism to declare symbolic names for values that won’t change during the execution of the program, such as the version number. In C++, programmers are encouraged to avoid #define in favor of using const to define constants. Defining a constant with const is just like defining a variable, except that the compiler guarantees that code cannot change the value.

const float kVersionNumber = 2.0f

const string kProductName = "Super Hyper Net Modulator";

const to Protect Parameters

In C++, you can cast a non-const variable to a const variable. Why would you want to do this? It offers some degree of protection from other code changing the variable. If you are calling a function that a coworker of yours is writing, and you want to ensure that the function doesn’t change the value of a parameter you pass in, you can tell your coworker to have the function take a const parameter. If the function attempts to change the value of the parameter, it will not compile.

In the following code, a char* is automatically cast to a const char* in the call to mysteryFunction(). If the author of mysteryFunction() attempts to change the values within the character array, the code will not compile. There are ways around this restriction, but using them requires conscious effort. C++ only protects against accidentally changing const variables.

void mysteryFunction(const char* myString);

int main()

{

char* myString = new char[2];

myString[0] = 'a';

myString[1] = '\0';

mysteryFunction(myString);

return 0;

}

void mysteryFunction(const char* myString)

{

myString[0] = 'b'; // Will not compile.

}

Code snippet from ConstArg\ConstArg.cpp

const References

You will often find code that uses const reference

Return Main Page Previous Page Next Page

®Online Book Reader