passing Temporary variables to reference arg in Constructor works. but not for functions in general. Why?

c++

Solution

It compiles because it's not creating an instance of `A`. It's declaring a function named `a` that returns an `A` and receives one unnamed parameter of type pointer-to-function-returning-B. Since it's just a declaration, it compiles. If you're referred to `a` elsewhere in the code, you'd have seen additional problems. For why that's a function declaration instead of an object definition, the term to look up is most vexing parse.

Problem

Consider the following code. Here, A a(B()) compiles even though the constructor is A(B& b); But print(B()) does not work. But print is also declared as print(B& b); Why this inconsistency? ``` #include <iostream> using namespace std; class B{ public: char b; }; class A { public: B b; A(B& b); A() { } }; A::A(B& b) { this->b = b; } void print(B& b) { } int main(){ print(B()); A a(B()); } ```

Original source

Related problems