If data is not going to be changed it is best to make it constant. If you declare a variable as constant and then later on in the code make a mistake and try to assign a new value to the variable the compiler will report the error of trying to modify a constant. On the other hand, if the variable not supposed to be changed, but you don't declare it as constant the compiler will not be able to catch your mistake of modifying it. It is far better to catch a mistake of this type at compile time rather than at runtime when the results are not as expected and you don't know why.
Data that will need to be modified later
cannot be made constant.
Why is not being "constant correct" considered "Bad Programming"? Consider the following example involving passing a parameter to a function by reference or constant reference. Suppose we have a function that takes a reference to a string and does not modify the string. Should we pass it by reference or constant reference?
1 2 3 4
|
void myFunction( std::string& s )
{
// function code here
}
|
or
1 2 3 4
|
void myFunction( const std::string& s )
{
// function code here
}
|
Note that the
const keyword guarantees that the function will not modify the string s.
Now suppose we call the function with this code snippet.
1 2
|
const std::string firstMonth = "January";
myFunction( firstMonth );
|
The first form above (without the keyword
const) will not compile because the compiler knows that
myFunction could possibly modify the string
firstMonth which is supposed to be constant. On the other hand the second form above (with the keyword
const) is just fine. You are passing
myFunction a string which is supposed to be constant and the function guarantees that it will not modify it.
Note that if
firstMonth was declared without the keyword const then both forms above would work or if we had a string that was inherently not constant both would still work. For example:
1 2 3 4
|
std::string s;
std::cout << "Please enter your last name: ";
std::cin >> name;
myFunction( name );
|
When first exposed to the concept of constant correctness it may seem counter intuitive that for a function that does not modify its reference parameter making the parameter a
const reference is better. Doesn't making it a constant make it more restrictive? The problem here is that there are two points of view.
From
inside the function the
const reference parameter is
more restrictive, i.e., the function can't modify the parameter. But from
outside the function, i.e., the code that calls the function, the
const reference makes it
less restrictive since it can be called with either a constant or a non-constant value.
Why not just never use the keyword
const? Because you lose the ability of the compiler to flag those programming errors where you have inadvertently modified a variable that shouldn't change. Being "constant correct" makes the compiler your friend and as I said above you should greatly prefer compile time errors over runtime errors.
As to your second question:
Can I make global classes in C++/CLI (window forms)? |
In C++ a class would be available for use in any file which includes the class header file. I am not very familiar with Microsoft's C++/CLI which is really a different language.