0

Found this by accident where a function declaration and definition may not agree on the constness of parameters. I've found some information (links following), but my question is why is const matching optional for by-value parameters, but const matching is required for reference parameters?

Consider the following code available here.

class MyClass
{
  int X;
  int Y;
  int Z;

public:
  void DoSomething(int z, int y, const int& x);
  int  SomethingElse(const int x);
  void Another(int& x);
  void YetAnother(const int& z);
};

void MyClass::DoSomething(int z, const int y, const int& x) // const added on 2nd param
{
  Z = z;
  Y = y;
  X = x;
}

int MyClass::SomethingElse(int x) // const removed from param
{
  X = x;
  x = 3;
  return x;
}

void MyClass::Another(int& x) // const not allowed on param
{
  X = x;
}

void MyClass::YetAnother(const int& z) // const required on param
{
  Z = z;
}

I've found this on SO, but it is looking for explanation for name mangling. I've also found this on SO and this on SO, but they don't go into detail on why const matching is required for reference parameters.

Community
  • 1
  • 1
bamakid
  • 13
  • 3
  • Mismatch is discussed [here](http://stackoverflow.com/q/20659000/1460794). – wally Apr 21 '16 at 20:50
  • 1
    This is not a duplicate question. I have not found a question regarding const on both by-value and by-reference and the reason for **why** there is a difference. Although [this](http://stackoverflow.com/questions/117293/use-of-const-for-function-parameters) has an answer that does cover by-reference it wasn't obvious since the question was for by-value and it doesn't answer **why** there is a difference. – bamakid May 06 '16 at 16:48

3 Answers3

3

When you pass by value, the argument is effectively a local variable of the function. Whatever you pass is copied. If the argument is a const T, it just means the function itself cannot modify its own variable. The caller shouldn't know or care about that.

Passing by const T& actually refers to access of a variable which does not belong to the function. Same with const T*. But not the same with T* const, that one would just mean the function itself cannot modify its own pointer. The pointer belongs to the function, if the function wants to reassign it to point to something else that's its own business. What it points to does not belong to the function, so whether the function gets const access or not is very relevant to the caller.

David
  • 27,652
  • 18
  • 89
  • 138
1

For value parameters, const actually makes no difference. If a parameter is passed as value, it cannot be modified by the function anyway.

Frank Puffer
  • 8,135
  • 2
  • 20
  • 45
1

If an argument is passed by value but marked as const, what would happen if the variable did get modified? The copy local to the function would change, but the copy that got passed in would not.

However, on the other hand if passed by reference, if you could somehow modify the variable it would manipulate the original and not just a copy.

fetherolfjd
  • 276
  • 1
  • 11