Shachar Shemesh <[EMAIL PROTECTED]> writes:

> There is no difference between the first a() and the second
> a(). Both are temporary variables of type a generated by an empty
> constructor.

Yes, there is a difference, and I suspect that is what you are
missing. In the first case you create a temp a() and call its
method. In the second case the compiler generates a temporary variable
to hold your a() and passes a reference to that temp to
func2. However, the compiler is only willing to generate a const temp
because it would be silly to modify a temp and immediately destroy it.

The equivalent code for your second line is

const a temp = a();
a::func2(temp);

The temp is necessary because you want to pass a reference to func2. A
reference can only be initialized with an lvalue (an object whose
address you can take). Your a() is not an lvalue (you cannot say a() =
something), but that's OK: you can initialize a const reference:

* convert the type to a if necessary (you don't need this in your
  example)

* place the result into a temp of type a

* use the temp as initializer

But - again! - this is only allowed for consts because otherwise you'd
be in a highly error-prone situation: you would think you are
modifying a variable, but instead you would be modifying a temp.

-- 
Oleg Goldshmidt | [EMAIL PROTECTED] | http://www.goldshmidt.org

=================================================================
To unsubscribe, send mail to [EMAIL PROTECTED] with
the word "unsubscribe" in the message body, e.g., run the command
echo unsubscribe | mail [EMAIL PROTECTED]

Reply via email to