Temporary objects

Aseem Rastogi aseem@india.tejasnetworks.com
Mon Jun 11 09:13:00 GMT 2007


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.



More information about the Gcc-help mailing list