Is changing overloaded variables from functions safe?

Is changing overloaded variables from functions safe? By that, I mean something like this

1
2
3
4
5
6
void function(int var) {

    var = 3+1

    return var;
}


or for an overloaded operator

1
2
3
4
5
6
7
bool Vector::operator>(const Vector& parameter) {

    parameter.x += 5;
    parameter.y += 5;

    return parameter.x >= 10 && parameter.y >= 10;
}
Last edited on
Well, in the first function you are taking var by value and thus a copy will be made. Not only that, but it is also not constant and thus it is perfectly legal to modify the value of var in the first function.

However, in your second example the code won't compile unless x and y are marked as mutable as you are trying to modify an argument that is marked as const. Even then, say those members are mutable, it doesn't generally make much sense to pass a variable into a function that marks said variable as constant only to break that promise.

Anyways, it is safe to change variables that are passed into functions so long as they are not marked as constant.
What do you mean by overloaded variable?

You can define the operators to do whatever you want, but if you do something unexpected there is an increased risk that someone will use it incorrectly.
Topic archived. No new replies allowed.