Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what's a variable with & -> double&

So I have a class made in c++ and need to convert it into vb.net but I got problem converting a certain part of a function.

void deg_min_sec2decimal(double deg, double min, double sec, double& dec_deg)
{
    dec_deg = (deg+min/60.0+sec/3600.0);
}

What type of variable is "double&", I know a double but what's with the "&"? It isn't " double &dec_deg" so it isn't an adres (pointers ect.)?

and how would i convert that to vb.net?

Grtz

like image 843
user727374 Avatar asked Sep 12 '11 13:09

user727374


3 Answers

It's a reference. That means that whatever you set the value to inside the function, the original variable that was passed in from outside will have that value.

You can convert this to VB by using ByRef or making the function return the result of that expression instead of taking the double&, and setting the variable you would have passed in to the result of the function.

So if you had this before:

double retval;
deg_min_sec2decimal(something, something2, something3, retval);

You'd change that to

Dim retval = deg_min_sec2decimal(something, something2, something3)

(I don't know VB so that syntax might be wrong, but you get the idea.)

I'd go with the latter because returning things through arguments is usually only used when you need more than one return value, and that function isn't even returning anything.

like image 96
Seth Carnegie Avatar answered Sep 21 '22 17:09

Seth Carnegie


double& is just a double passed by reference. In VB.NET, it would be declared ByRef dec_deg as Double.

EDIT: However, I would recommend instead of using a void function to set a value by reference, just change the return type to double and return the expression instead of having to pass a variable by reference.

like image 41
Michael Avatar answered Sep 20 '22 17:09

Michael


double& is a reference to a double. This means in that case that the function can change the value of the passed parameter.

In VB, you do this by ByRef parameters:
Sub deg_min_sec2decimal(deg as Double, min as Double, sec as Double, ByRef dec_deg as Double)

like image 30
king_nak Avatar answered Sep 18 '22 17:09

king_nak