Temporary objects

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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 of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux