This question doesn't completely help me to clarify this issue. Say I have the function:
int foo(){
/* some code */
return 4;
}
int main(){
foo();
}
Since I'm returning by value, will a copy of the integer returned by foo() be made? This answer mentions that the compiler will turn the function into a void function. Does that mean the actual function being called will be this one?
void foo(){
/* some code */
}
This question is related to using a class method as a helper function and an interface function at the same time. For instance, assume the classes Matrix
and Vector
are defined.
class Force
{
public:
Matrix calculate_matrix(Vector & input);
Vector calculate_vector(Vector & input);
private:
Matrix _matrix;
Vector _vector;
};
Implementation
Matrix Force::calculate_matrix(Vector & input){
/* Perform calculations using input and writing to _matrix */
return _matrix;
}
Vector Force::calculate_vector(Vector & input){
calculate_matrix(input)
/* Perform calculations using input and _matrix and writing to _vector */
return _vector;
}
Whenever I call Force::calculate_matrix()
in Force::calculate_vector()
, am I making a copy of _matrix
? I need Force::calculate_matrix()
as an interface whenever I just need to know the value of _matrix
. Would a helper function such as:
void Force::_calculate_matrix(Vector & input){
/* Perform calculations using input and writing to _matrix */
}
and then use it in Force::calculate_matrix()
and Force::calculate_vector()
be a better design solution or I can get away with the compiler optimizations mentioned above to avoid the copies of the Force::calculate_matrix()
returning value?