P
Paul N
This is probably a basic question - I just have nagging doubts about
using temporaries.
Suppose I have a struct consisting of a number of values I want to
pass to a function together - for example, the size and position of a
window. If I do:
MyRect getsize() {
MyRect temp;
temp.x = 3; // etc
return temp; }
then this will, I think, return a temporary MyRect. As I see it, I can
use it either as:
void fun1(MyRect in); or void fun2(const MyRect& in);
and so can do
fun1(getsize()); or fun2(getsize());
Am I right in thinking that either of these will work, assuming the
functions only read the values in the MyRect? And that the second form
is preferred, as (potentially) requiring less copying?
And does it make a difference if the function is in fact a
constructor? For example:
Note::Note(const MyRect& in) : m_x(in.x) { }
n = new Note(getsize());
Thanks for any advice.
Paul.
using temporaries.
Suppose I have a struct consisting of a number of values I want to
pass to a function together - for example, the size and position of a
window. If I do:
MyRect getsize() {
MyRect temp;
temp.x = 3; // etc
return temp; }
then this will, I think, return a temporary MyRect. As I see it, I can
use it either as:
void fun1(MyRect in); or void fun2(const MyRect& in);
and so can do
fun1(getsize()); or fun2(getsize());
Am I right in thinking that either of these will work, assuming the
functions only read the values in the MyRect? And that the second form
is preferred, as (potentially) requiring less copying?
And does it make a difference if the function is in fact a
constructor? For example:
Note::Note(const MyRect& in) : m_x(in.x) { }
n = new Note(getsize());
Thanks for any advice.
Paul.