@phoenix

The reason is not an implication of using references.
If u are passing emptyvec() as an argument then the vector returned by 
emptyvec() is a temp object ( as its not being assigned to a obj var 
created by the programmer), which means that the user/programmer shouldn't 
be able to change it. Because temp objects are not under the control or 
rather can't be explicitly created/handled by the programmer. Hence, it 
won't allow u to modify the temp object. Now when a temp object is assigned 
to a const ref, it automatically means that u can use but can't modify it 
which is expected. But, if its a non-const ref, then that means one can 
modify a temp object which ideally a programmer doesn't have a control on.
 


On Sunday, 23 December 2012 07:33:48 UTC+5:30, phoenix wrote:
>
> Hi,
>
> Could someone explain the logic behind the following:
>
> "Arguments that correspond to non-const reference parameters must be 
> lvalues-that is they must be non-temporary objects. Arguments that are 
> passed by value or bound to a const reference can be any value"
>
> Suppose a function returns an empty vector
> vector<double> emptyvec()
> {
>  vector <double>v;
> return v;
> }
>
> Now calling another function which accepts const reference as a parameter 
> does not error out but function which accepts non-const reference parameter 
> errors when passing emptyvec() to it.Why? I do not understand the concept.
>
> This emptyvec() function returns a copy of v to the caller and destroys v 
> which is local to emptyvec(). So why cannot this copy be passed without 
> error as a non-const reference. Why is the behavior different between const 
> and non-const reference.?
>  
>  

-- 


Reply via email to