Re: Questions about chapter 11 (Operator Overloading) of TC++PL.
"Rajesh S R" <SRRajesh1989@gmail.com> wrote in message
news:1193164518.044045.219590@q5g2000prf.googlegroups.com...
On Oct 23, 2:09 pm, anon <a...@no.no> wrote:
Wayne Shu wrote:
Hi everyone, I am reading B.S. 's TC++PL (special edition).
When I read chapter 11 Operator Overloading, I have two questions.
1. In subsection 11.2.2 paragraph 1, B.S. wrote "In particular,
operator =, operator[], operator(), and operator-> must be nonstatic
member function; this ensures that their first operands will be
lvalues". I know that these operators must be nonstatic member
functions, but why this ensure their first operands will be lvalues?
e.g.
class foo
{
int i;
public:
foo & operator = (int ii) { i = ii; return *this; }
The first operant is ii.
};
foo bar() { return foo(); }
bar() = 10;
I wonder if this is legal?
The above statement is equivalent to:
bar().operator=(10);
bar() produces a temporary object of type foo. Its member is modified
by the above statement. Is it legal to change the value of temporary
objects created in a full expression, as pre C++ standards?
Some more clarifications are:
Can I evaluate the address of such temporary objects?
I mean, can I do:
std::cout << &bar();
Please give me the relevant sections of C++03 which deals with the
temporary objects created within a full expression.
I know it is legal to pass a constant reference to a temporary object. I.e.
void Somefunction( const bar& b )
{
}
Somefunction( bar() );
becasue it is ILLEGAL to pass a non constant refernece to a temporary
object.
A temporary object has a lifetime, although it is rather short, generally
one statement. I can't give you chapter and verse, because I don't have a
copy of the standard, but afaik
bar() = 10;
is perfectly legal.