If I pass a variable to a function but mark that variable as constant will it automatically get passed by reference?
In C++
void foo(const int a)
After testing: No because it prints different memory locations.
void foo(const int a)
{
cout << &a << endl;
}
int main()
{
int a = 5;
foo(a);
cout << &a << endl;
return 0;
}
C
No right? Because I can only pass a pointer to the memory location, not really by reference. Like here: Passing by reference in C
Java
Maybe the compiler can figure out that final
should point to the same memory location?
void foo(final int a)
C#
Same as Java?
void foo(const int a)
I know I included 4 languages in my question but since I use those 4 languages a lot I want to know.
Is there a way to force Java and C# to handle do this? I know in C++ I can add &
to the function and it will be exactly what I want.
void f(const int &a)
Pretty much that is what I am hopping Java and C# compilers are doing behind the scenes.