I'm new to C++ and trying to understand something. I have this code in my main.cpp:
Radio r = Radio("PSR", 100.8);
or that code:
Radio r("PSR", 100.8);
Both seem to work and doing the same thing. So what's the difference?
I'm new to C++ and trying to understand something. I have this code in my main.cpp:
Radio r = Radio("PSR", 100.8);
or that code:
Radio r("PSR", 100.8);
Both seem to work and doing the same thing. So what's the difference?
Copyright © 2021 Jogjafile Inc.
Radio r = Radio("PSR", 100.8);is copy initialization whileRadio r("PSR", 100.8);is direct initialization.C++17
From C++17 due to mandatory copy elison both are the equivalent.
Prior C++17
But prior to C++17, the first case
Radio r = Radio("PSR", 100.8);may result in the creation of a temporary using whichris copy initialized. This is because prior to C++17, there was non-mandatory copy elison.Another thing to note is that if you were to write:
the above is a declaration for a function named
namewhich has the return type oftypeand has0parameters.