Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

What is & in object creation?

I came across some C++ code of the form below, where Foo is an object:

Foo y;
[...]
const Foo & x = y;

I haven’t seen this usage of & before. What search terms can I use to learn about this usage of &?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

const Foo & and const& Foo are all the same.

They declare that x is a const reference to y. Operations called on x will be called on y, but x can only access const operations.

(Though there are some syntactical quirks that can arise if Foo is a pointer)

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading