Link passing problem

Is it possible to overload a function that takes the name of a link or variable?

For example, when I try to do this:

void function(double a); void function(double &a); 

I would like the calling function of this function to be able to:

 double a = 2.5; function(a); // should call function(double &a) function(2.3); // should call function(double a) 

I would like to write link-passing functions for better memory usage and possible manipulation of a variable out of scope, but without the need to create a new variable so that I can call the function.

Is it possible?

Greetings

+7
c ++ pass-by-reference pass-by-value overloading
source share
6 answers

I think you are missing a point here. What you really need is simple:

 void function(const double &a); 

Pay attention to "const". In this case, you should always receive bandwidth information. If you have a non-const pass by reference, then the compiler correctly assumes that you want to modify the passed object, which, of course, is conceptually incompatible with the pass-by-value option.

With links to const links, the compiler will happily create a temporary object for you behind your back. The non-const version does not work for you, because the compiler can only create these temporary objects as const objects.

+8
source share

I tried and he couldnโ€™t

At least in MSVC2008 this is not possible - and I think this applies to all C ++ compilers.

The fact itself is determined, but when trying to call a function

 function(a); 

with a variable as a parameter, a compiler error occurs because the compiler cannot decide which function to use.

+1
source share
 void function(double const &a); // instead of void function(double a); void function(double &a); 
+1
source share

I do not think the definition:

 void function(double a); void function(double &a); 

maybe ... How does the compiler know which function to call for function(x) ? If you want to use both by reference and by value, you must use two different functions. It will also make it easier to read the code than this ambiguous overload.

0
source share

It is not possible to pass POD types by reference, since they are almost always (always?) Passed in the registry. Complex types (those that consist of more than one POD variable) that are passed by a constant reference are usually (always ?: D) a proactive way to copy and stack. You can even control the creation of time series of your complex typed variables from POD types by creating explicit constructors

0
source share

No, you cannot do this, both functions will effectively create the same malformed names, and the linker will not be able to solve the function call.

-one
source share

All Articles