   Passing function return as reference parameter
  There's some commentary in the code below which explains my problem.  Please help me understand why this doesn't work.class Cat{    public:   Cat() {};};class House{    public:   House(Cat&);    private:   Cat m_HouseCat;};House::House(Cat& HouseCat)    : m_HouseCat( HouseCat ){}Cat functionA(){    Cat Kitty;    return Kitty;  // This returns by value so it's not a local temporary, correct?}int main(){    House MyHouse( Cat ); // This works.    Cat FatCat;    House HisHouse( FatCat );  // So does this.    House HerHouse( functionA() );  // The error is here. // No matching function for call to 'House::House(Cat)' // Candidate is House::House(Cat&). // Why?  I can't see the difference between sending 'FatCat' // and sending the return from functionA.}
  The return value from a function is a temporary, temporaries can't be bound to non-const references. In this case you can probably fix your code by changing the House constructor to take a const Cat &.   ;  I know you don't change the Cat from within the House constructor, but as it stands, you're allowed to (i.e. you didn't promise, via 'const', that you wouldn't). Consider what would happen if you did: there would be a change to the temporary value 'functionA()', but no way to *see* that change in the calling function (because it's a temporary). C++ protects you from accidentally throwing away changes by requiring you to not make them except where you can inspect them.   ;Quote:Original post by ZahlmanC++ protects you from accidentally throwing away changes by requiring you to not make them except where you can inspect them.It'd be more accurate to say "C++ tries to protect you". There are some loopholes that can get you. (Or that you can exploit, depending on your point of view.)   