What's the difference?
-
class Integer {
int i;
public:
Integer(int ii) : i(ii) {}
const Integer operator+(const Integer& rv) const
{
cout << "operator+" << endl;
return Integer(i + rv.i);
}
};Regarding the codes above what's the difference between
...
return Integer(i + rv.i);
...and
...
Interger a(i + rv.i);
return a;
...In addition in what context should one of these syntax be used?
-
class Integer {
int i;
public:
Integer(int ii) : i(ii) {}
const Integer operator+(const Integer& rv) const
{
cout << "operator+" << endl;
return Integer(i + rv.i);
}
};Regarding the codes above what's the difference between
...
return Integer(i + rv.i);
...and
...
Interger a(i + rv.i);
return a;
...In addition in what context should one of these syntax be used?
the only difference remains in the fact that in first last sample, you create a temporary variable, which only exists to return its content. it is so avoided if you want to optimize your code. the only way it could be useful is when debugging, by setting a breakpoint at this point, you can control how the temporary variable is created. that's all.
TOXCCT >>> GEII power
[toxcct][VisualCalc] -
the only difference remains in the fact that in first last sample, you create a temporary variable, which only exists to return its content. it is so avoided if you want to optimize your code. the only way it could be useful is when debugging, by setting a breakpoint at this point, you can control how the temporary variable is created. that's all.
TOXCCT >>> GEII power
[toxcct][VisualCalc]I modified the forward example a lit bit. In which constructor, copy-constructor and destructor announce themselves!
...
Interger a(i + rv.i);
return a;
...Its output was constructor -> copy-constructor(cuz return by value) -> destructor(local object goes out of scope) -> destructor(temp object generated by compiler which was to hold returned object got destructed)
...
return Integer(i + rv.i);
...Its output was constructor -> destructor I pretty clear with the former's process and obviously it has a much more overhead compare to the latter, but I wonder since there's no CC calling in the former, how can I get the returned value?What exactly the process the former is taking???
-
I modified the forward example a lit bit. In which constructor, copy-constructor and destructor announce themselves!
...
Interger a(i + rv.i);
return a;
...Its output was constructor -> copy-constructor(cuz return by value) -> destructor(local object goes out of scope) -> destructor(temp object generated by compiler which was to hold returned object got destructed)
...
return Integer(i + rv.i);
...Its output was constructor -> destructor I pretty clear with the former's process and obviously it has a much more overhead compare to the latter, but I wonder since there's no CC calling in the former, how can I get the returned value?What exactly the process the former is taking???
In the first case, the compiler creates a temporary object on the stack (in the function, object a). You return this object by value, so the compiler creates another temporary object on the stack (the return value) and then calls the copy constructor to copy object a into the temporary object. Than, object a gets destroyed and after finishing the statement, the temporary return value gets destroyed also. In the second case, you introduce no temoray variable in the function itself. You call the contructor to return the value. In this case, the destructor of the returned temporary variable is called. Behind every great black man... ... is the police. - Conspiracy brother Blog[^]
-
In the first case, the compiler creates a temporary object on the stack (in the function, object a). You return this object by value, so the compiler creates another temporary object on the stack (the return value) and then calls the copy constructor to copy object a into the temporary object. Than, object a gets destroyed and after finishing the statement, the temporary return value gets destroyed also. In the second case, you introduce no temoray variable in the function itself. You call the contructor to return the value. In this case, the destructor of the returned temporary variable is called. Behind every great black man... ... is the police. - Conspiracy brother Blog[^]
-
I modified the forward example a lit bit. In which constructor, copy-constructor and destructor announce themselves!
...
Interger a(i + rv.i);
return a;
...Its output was constructor -> copy-constructor(cuz return by value) -> destructor(local object goes out of scope) -> destructor(temp object generated by compiler which was to hold returned object got destructed)
...
return Integer(i + rv.i);
...Its output was constructor -> destructor I pretty clear with the former's process and obviously it has a much more overhead compare to the latter, but I wonder since there's no CC calling in the former, how can I get the returned value?What exactly the process the former is taking???
by making
return Integer(i + rv.i);
the compiler creates a temporary object which is initalized by its constructor (called here with the parameter i + rv.i). then, by executing return the program exits the local scope of the function and so calls the destructors of the local variables... nothing more, nothing less.
TOXCCT >>> GEII power
[toxcct][VisualCalc] -
class Integer {
int i;
public:
Integer(int ii) : i(ii) {}
const Integer operator+(const Integer& rv) const
{
cout << "operator+" << endl;
return Integer(i + rv.i);
}
};Regarding the codes above what's the difference between
...
return Integer(i + rv.i);
...and
...
Interger a(i + rv.i);
return a;
...In addition in what context should one of these syntax be used?
Actually, there is no real diference. Do whatever is clearer to you.... Any decent compiler is intelligent enough to optimise away the temporary variables. The compiler does a much better job at low-level optimisation than any programmer, I usually leave this job to it and keep the code the clearest for me. My 2 cents.. Guy :)
-
class Integer {
int i;
public:
Integer(int ii) : i(ii) {}
const Integer operator+(const Integer& rv) const
{
cout << "operator+" << endl;
return Integer(i + rv.i);
}
};Regarding the codes above what's the difference between
...
return Integer(i + rv.i);
...and
...
Interger a(i + rv.i);
return a;
...In addition in what context should one of these syntax be used?
no real difference. But depending on the return type of the function, the second bit of code could create another temporary. For instance:
int function() { return Integer(i + rv.i); }
andint function() { Integer a(i + rv.i); return a; }
will both create a temporary object (even 'a' could be considered temporary) and return the encapsulatedint
value. WhileInteger function() { return Integer(i + rv.i); }
andInteger function() { Integer a(i + rv.i); return a; }
Will both create TWO temporary objects, one inside the function, and one as the return value.