This is the mail archive of the gcc-help@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

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.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]