R
Russ
I just ran into something that absolutely blows my mind! Consider the
following:
class Class1 {
public:
void SetHours (const double& h) { Hours = h; }
double Hours;
};
public __gc class Class2 {
public:
double Hours;
};
Now, in C++ Managed extensions do this:
Class1 c1;
Class2 c2;
c1.Hours = c2.hours; // Works fine
c1.SetHours (c2.Hours); // Fails
The error is "C2664: 'Class1::SetHours' : cannot convert parameter 1
from 'double' to 'const double &'".
Say What!!??!!
Somewhere it says this is because c2.Hours is a managed type. Why
should that matter? This breaks the whole const mechanism. In the
case above I can get around it by using the direct copy (c1.Hours =
c2.hours), but suppose the members of Class1 were protected or
private? The whole reason for using const is to tell the compiler
that it cannot modify the input value. Why in the world would
Microsoft want to break this mechanism????
To get around this I must modify all the access member functions of my
unmanaged DLL to not use const? Or create a whole bunch of
intermediate variables and do a double copy? It does not make any
sense. Why doesn't the compiler handle that job automatically?
Russ
following:
class Class1 {
public:
void SetHours (const double& h) { Hours = h; }
double Hours;
};
public __gc class Class2 {
public:
double Hours;
};
Now, in C++ Managed extensions do this:
Class1 c1;
Class2 c2;
c1.Hours = c2.hours; // Works fine
c1.SetHours (c2.Hours); // Fails
The error is "C2664: 'Class1::SetHours' : cannot convert parameter 1
from 'double' to 'const double &'".
Say What!!??!!
Somewhere it says this is because c2.Hours is a managed type. Why
should that matter? This breaks the whole const mechanism. In the
case above I can get around it by using the direct copy (c1.Hours =
c2.hours), but suppose the members of Class1 were protected or
private? The whole reason for using const is to tell the compiler
that it cannot modify the input value. Why in the world would
Microsoft want to break this mechanism????
To get around this I must modify all the access member functions of my
unmanaged DLL to not use const? Or create a whole bunch of
intermediate variables and do a double copy? It does not make any
sense. Why doesn't the compiler handle that job automatically?
Russ