M
Michael Safyan
Dear members of comp.lang.c++,
I am a little bit confused about the differences between constant
references and values. I understand that it is faster to use a constant
reference ("const T&") than a value ("T") since the former does not
require copying whereas the latter does, is that correct? Also, I un
derstand that "const T&" allows for polymorphism whereas "T" will
generate code cuttting.
On the former points, I am fairly confident... But I'm somewhat
confused on how the two influence conversions. For example, suppose I
define the following type:
class Real{
public:
Real(int)
Real(float);
Real(double);
Real(long double);
...
Real operator+(Real);
};
Can I now write the following?
Real x = 5.0;
Real y = x + 3;
//is above legal now if I don't define Real:perator+(int)?
Now, suppose I change the declaration+definition of
Real:perator+(Real) to Real:perator+(const Real&), is the above
still legal? Or do I need to modify the above to the following?
Real x = 5.0;
Real tmp = 3;
Real y = x + tmp;
**************************************
In short, can one convert an unnamed constant to a constant reference or
only to a value?
I am a little bit confused about the differences between constant
references and values. I understand that it is faster to use a constant
reference ("const T&") than a value ("T") since the former does not
require copying whereas the latter does, is that correct? Also, I un
derstand that "const T&" allows for polymorphism whereas "T" will
generate code cuttting.
On the former points, I am fairly confident... But I'm somewhat
confused on how the two influence conversions. For example, suppose I
define the following type:
class Real{
public:
Real(int)
Real(float);
Real(double);
Real(long double);
...
Real operator+(Real);
};
Can I now write the following?
Real x = 5.0;
Real y = x + 3;
//is above legal now if I don't define Real:perator+(int)?
Now, suppose I change the declaration+definition of
Real:perator+(Real) to Real:perator+(const Real&), is the above
still legal? Or do I need to modify the above to the following?
Real x = 5.0;
Real tmp = 3;
Real y = x + tmp;
**************************************
In short, can one convert an unnamed constant to a constant reference or
only to a value?