Is this stuff necessary?

I see this a lot in people's code and I'm wondering, is it something I should be taking up on or does it not matter.

This:
void SomeFunction( ) const; // <-- the "const"

And:

void SomeFunction(myType& someParameter) <-- the address of operator
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).
Alright thanks. So, essentially, use "&" as much as possible?
Usually. If you need a copy of the variable then obviously you wouldn't use it. Also as an FYI, types like int, char are inexpensive to copy so you don't have to use a & with them.
void SomeFunction( ) const; // <-- the "const"

Think of it this way. When you are designing a program for a user, you want to give him/her just enough freedom to successfully use the program, but no more than that. Everything else you want to control in order to ensure that some new source of error won't be introduced into your code. Specifying constant member function is a way to guarantee that the object's state isn't being changed.
Kangaroux wrote:
So, essentially, use "&" as much as possible?


No, use it where appropriate. In your example:

void SomeFunction(myType& someParameter);

someParameter, because it is non-const, is an output parameter. If the input value of someParameter is not used, it may have been possible to just write the function as:

myType someFunction();

Returning the result by value. In general that is preferred for clarity. However, the first may be a lot more efficient if the compiler cannot do return value optimization (RVO).

Pass input parameters by const reference when possible. However, if you need to make a copy of the input parameter in your function, you might as well just pass by value.

Return by value where possible. However, if the code cannot be optimized properly by the compiler and efficiency is paramount, you may need to return by reference.

Hope that helps.
You can also combine the two concepts like so in order to guarantee to the caller that the input parameter won't be changed. This saves you from having to copy large objects.
void SomeFunction(const myType& someParameter);
Topic archived. No new replies allowed.