Hi Guys,
I have a question on C++ object creation. Please consider the following code snippet.

class Foo {
private: int i;
public:  A(int ii):i(ii) {};
};
int main() {
 const Foo &ref = Foo(1);
}

Here I am bit confused about the line :
const Foo &ref = Foo(1);
Could anybody please explain me all behind the scene actions?
Why "const" key word is required here? How does this line is interpreted by compiler? We are not using "new" keyword here so I am bit confused how reference is returned?

Thank you.

>Why "const" key word is required here?

Temporary object.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.