The const is const correctness, and yes you should take it up.
A const member function is like a guarantee that it will not change the state of the object. Therefore, you can only call const member functions from a const object.
Example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
|
class Foo
{
public:
void func_const() const { }
void func_nonconst() { }
};
int main()
{
// if you have a nonconst object, then you can call nonconst and const
// functions
Foo a;
b.func_const(); // works OK
a.func_nonconst(); // works OK
// but if you have a const object, you can only call const functions
const Foo b;
b.func_const(); // works OK
b.func_nonconst(); // ERROR, can't call nonconst function with const object
}
|
The "address of operator" example is not the address of operator. It's a reference.
References are a fundamental concept. Basically a reference is a shortcut or alias to another variable.
For example:
1 2 3 4 5 6
|
int a = 0;
int& b = a; // now b *IS* a
b = 5; // since b *IS* a, this is the same as "a = 5"
cout << a; // this prints '5'
|
This is commonly used in functions paramters like in your example. If you pass "by value" (
void func(myType foo)
), then the passed parameter is really a
copy of the value passed to the function. Copies can sometimes be expensive, so this can be avoided by passing it "by reference" instead (
void func(const myType& foo)
). Now 'foo' is a const reference, which means you didn't create a copy of the passed value. Instead, you're using the passed value directly.
The 'const' ensures the function will not change that value. But you can leave out the const and have the function change the value if you want (that might be the desired effect of the function).