This is the mail archive of the
gcc-help@gcc.gnu.org
mailing list for the GCC project.
Temporary objects
- From: Aseem Rastogi <aseem at india dot tejasnetworks dot com>
- To: gcc-help at gcc dot gnu dot org
- Date: Mon, 11 Jun 2007 12:51:22 +0530
- Subject: Temporary objects
Hi All,
I find following C++ behaviour regarding temporary objects quite
contradictory. Consider this:
class A
{
...
public:
void bar () { ... } // non-const member function
};
A getA () { ... } // return an object
void foo (A& a) { ... }
int main ()
{
foo (getA ()); // error: not allowed to pass temporary object as
non-const reference. acceptable.
getA ().bar (); // why is this allowed ?? calling a non-const
functrion on a temporary object ?
}
Any ideas why it could be so ?
If passing a temporary object as non-const reference is not allowed
because making changes to it doesn't make sense, doesn't same thing
apply to calling non-const member function on a temporary ?
Thanks in advance,
-Aseem.