Hello every body,
I am a newbie to c++ and I am wondering about the differences of introducing variables in these two ways:
1. double a
2. double a(0)
My another question is about this swaping code:
void swap (double &a ,double &b)
{
double t(a);
a=b;
b=t;
}
Firstly I don't understand this t(a) and secondly I don't understand what would have been the difference if I introduce a,b without their reference to the function!
1 declares a double "a". now it has some random value. 2 declares a double "a" and sets it to 0. It's called a copy constructor. It's equivalent to double a = 0;
double t(a) again is equivalent to double t = a. Note however that this is equivalent only as you declare the variable.
If "a" and "b" had no references on them, they would be passed as copies, and and changes made to those copies would have no effect on the rest of your program.