J
JKop
Firstly, we all know that temporaries are non-const, which I shall
demonstrate with the following code:
struct Blah
{
int monkey;
void SetMonkey(int const supplied)
{
monkey = supplied;
}
};
Blah SomeFunc()
{
return Blah();
}
int main()
{
Blah().SetMonkey(5);
SomeFunc().monkey = 5;
}
But, at the same time, temporaries are "r-value"'s. The following is
illegal:
int Blah()
{
return 5;
}
int main
{
Blah() = 6;
}
Also, in the previous code, "Blah().monkey = 5" is illegal.
It's not legal to bind a temporary to a non-const reference, because it is
an "r-value".
You're allowed to use "const_cast" to cast away the constness if the
original object was in fact non-const...
struct Blah
{
int a;
double b;
char c;
float** p_p_f;
wchar_t k;
bool f;
};
int main()
{
Blah const &poo = Blah();
Blah &cow = const_cast<Blah&>(poo);
cow.a = 5;
}
Any thoughts on this?
-JKop
demonstrate with the following code:
struct Blah
{
int monkey;
void SetMonkey(int const supplied)
{
monkey = supplied;
}
};
Blah SomeFunc()
{
return Blah();
}
int main()
{
Blah().SetMonkey(5);
SomeFunc().monkey = 5;
}
But, at the same time, temporaries are "r-value"'s. The following is
illegal:
int Blah()
{
return 5;
}
int main
{
Blah() = 6;
}
Also, in the previous code, "Blah().monkey = 5" is illegal.
It's not legal to bind a temporary to a non-const reference, because it is
an "r-value".
You're allowed to use "const_cast" to cast away the constness if the
original object was in fact non-const...
struct Blah
{
int a;
double b;
char c;
float** p_p_f;
wchar_t k;
bool f;
};
int main()
{
Blah const &poo = Blah();
Blah &cow = const_cast<Blah&>(poo);
cow.a = 5;
}
Any thoughts on this?
-JKop