D
DaKoadMunky
Please consider the following...
<CODE>
#include <string>
using namespace std;
typedef int PrimitiveType;
typedef string ClassType;
PrimitiveType ReturnPrimitiveType()
{
PrimitiveType result;
return result;
}
ClassType ReturnClassType()
{
ClassType result;
return result;
}
int main()
{
PrimitiveType pt;
ClassType ct;
ReturnPrimitiveType() = pt; //ERROR!
ReturnClassType() = ct; //OKAY!
return 0;
}
</CODE>
The first function call in main() generates the error "left operand must be
l-value."
The second function call in main() does not.
Both functions return their results by value. The only difference between the
functions is that the first returns a primitive type and the second returns a
class type.
Both are obviously temporary objects, but my compiler seems to consider the
temporary of class type to be an l-value.
The primitive return type seems implicitly const whereas with the class type
return type I need to explicitly specify const.
Is that the correct behavior?
Why allow modification of the class type temporary but disallow it for the
primitive temporary?
It is not important to some specific problem I have, I just like knowing why
languages work the way they do.
Regards,
Brian
<CODE>
#include <string>
using namespace std;
typedef int PrimitiveType;
typedef string ClassType;
PrimitiveType ReturnPrimitiveType()
{
PrimitiveType result;
return result;
}
ClassType ReturnClassType()
{
ClassType result;
return result;
}
int main()
{
PrimitiveType pt;
ClassType ct;
ReturnPrimitiveType() = pt; //ERROR!
ReturnClassType() = ct; //OKAY!
return 0;
}
</CODE>
The first function call in main() generates the error "left operand must be
l-value."
The second function call in main() does not.
Both functions return their results by value. The only difference between the
functions is that the first returns a primitive type and the second returns a
class type.
Both are obviously temporary objects, but my compiler seems to consider the
temporary of class type to be an l-value.
The primitive return type seems implicitly const whereas with the class type
return type I need to explicitly specify const.
Is that the correct behavior?
Why allow modification of the class type temporary but disallow it for the
primitive temporary?
It is not important to some specific problem I have, I just like knowing why
languages work the way they do.
Regards,
Brian