Is this const in the function parameter redundant?

If I add a const before a function parameter, that means I will not change the parameter within the function:

int get_age(const Dog& dog) {
return dog.age();
}


Furthermore, if I add a const at the end of a function signature, then I will not change any variables in the function:

int get_age(Dog& dog) const {
return dog.age();
}


Hence, is the const before the parameter redundant in this case since the const after the function signature already guarantees that I will change no variables in the function?

int get_age(const Dog& dog) const {
return dog.age();
}
In point two, you can't add const at the end of a nonmember function.

Adding it to the end of a member function guarantees that non of the member variables in the in the class will be modified.
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
struct MyClass
{
    int x;
    void f1(YourClass &yc)
    {
        //yc can be modified
        //x can be modified
    }
    void f2(YourClass const &yc)
    {
        //yc cannot be modified
        //x can be modified
    }
    void f3(YourClass &yc) const
    {
        //yc can be modified
        //x cannot be modified
    }
    void f4(YourClass const &yc) const
    {
        //yc cannot be modified
        //x cannot be modified
    }
};
closed account (zb0S216C)
The "const" in either context mean the same thing more or less.

If a parameter is a reference to a constant ("const Dog &dog") it allows temporary instances [of "Dog"] to be bound to it [the reference]. However, if the parameter becomes constant; its state can no longer change. For example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
void FunctionA( Class &InstanceA )
{
  InstanceA.Member = ...;
}

void FunctionB( Class const &InstanceB )
{
  InstanceB.Function( );
}

int main( )
{
  FunctionA( ( Class( ) ) ); // Error: expected an l-value instance of "Class".
  FunctionB( ( Class( ) ) ); // OK

  Class Instance;
  FunctionB( Instance ); // OK, but "Instance" cannot be modified by "FunctionB( )".
}

If a temporary "Class" instance is passed to "FunctionB( )", the temporary instance will have its lifetime match that of the "InstanceB" parameter of "FunctionB( )". In other words, when "InstanceB" goes out of scope at the end of "FunctionB( )" so will the temporary instance.

If a member-function is constant, it tells the compiler that the function will not alter the state of the instance pointed to by "this"; the "const" after a function's parameter list modifies the declaration of the "this" parameter. For example:

1
2
3
4
5
6
7
8
9
10
11
12
13
// "this" is implicitly added by the compiler, but I've added it for clarity.

// Without the "const" qualifier:
void Class::Function( Class * const this, int Parameter )
{
  // "this" is a constant pointer ["this" isn't allowed to be reseated] to a non-constant "Class".
}

// With the "const" qualifier:
void Class::Function( Class const * const this, int Parameter ) const
{
  // "this" is a constant pointer to a constant "Class".
}

Note the difference in the two declarations of "this". However, if a data-member is mutable, the function is permitted to modify the [mutable] data-member.

Wazzak
Last edited on
Topic archived. No new replies allowed.