Re: returning lvalue in C vs C++
- From: Kai-Uwe Bux <jkherciueh@xxxxxxx>
- Date: Wed, 29 Mar 2006 19:35:09 -0500
Ben C wrote:
On 2006-03-29, Ben C <spamspam@xxxxxxxxx> wrote:
On 2006-03-29, Phlip <phlip2005@xxxxxxxxx> wrote:
Ben C wrote:
struct thing
{
int x;
};
struct thing f(void)
{
struct thing ret = {100};
return ret;
}
int g(void)
{
return 10;
}
int main(void)
{
#if 0
g() = 4; /* "invalid lvalue" in C and C++ */
#endif
f().x = 200; /* "invalid lvalue" in C, but not in C++ apparently
*/
Consider the general scheme of C++, and specifically its overloaded
operators.
x.operator=() (figuratively) could have a side-effect that your code
depends on. So regardless where 'thing' appears, thing.x is accessible
as an lvalue.
It would be thing.operator=() but I see the point.
Sorry, no it wouldn't, you were right. It's x's operator= we'd be
calling.
But x is an int, so if f().x = 200 works on that basis, why not g() = 4?
Yup: why not? I don't know. But for classes, it sure works:
#include <iostream>
struct dummy {
dummy & operator= ( dummy const & other ) {
std::cout << "hello world!\n";
return ( *this );
}
};
dummy g ( void ) {
dummy tmp;
return ( tmp );
}
int main ( void ) {
g() = g();
}
Best
Kai-Uwe Bux
.
- Follow-Ups:
- Re: returning lvalue in C vs C++
- From: Alf P. Steinbach
- Re: returning lvalue in C vs C++
- References:
- returning lvalue in C vs C++
- From: Ben C
- Re: returning lvalue in C vs C++
- From: Phlip
- Re: returning lvalue in C vs C++
- From: Ben C
- Re: returning lvalue in C vs C++
- From: Ben C
- returning lvalue in C vs C++
- Prev by Date: Re: returning lvalue in C vs C++
- Next by Date: Re: returning lvalue in C vs C++
- Previous by thread: Re: returning lvalue in C vs C++
- Next by thread: Re: returning lvalue in C vs C++
- Index(es):
Relevant Pages
|