![]() |
Eliminating Temporary Objects
C++ creates temporary objects "behind your back" in several contexts. The overhead of a temporary can be significant because both its constructor and destructor are invoked. You can prevent the creation of a temporary object in most cases, though. In the following example, a temporary is created:
Code: c
The expression y+z; results in a temporary object of type Complex that stores the result of the addition. The temporary is then assigned to x and destroyed subsequently. The generation of the temporary object can be avoided in two ways: Code: c
In the example above, the result of adding x and z is constructed directly into the object x, thereby eliminating the intermediary temporary. Alternatively, you can use += instead of + to get the same effect: Code: c
Although the += version is less elegant, it costs only two member function calls: assignment operator and operator +=. In contrast, the use of + results in three member function calls: a constructor call for the temporary, a copy constructor call for x, and a destructor call for the temporary. |
Re: Eliminating Temporary Objects
Though this topic is very debatable. But I'll keep out of it.
Nice work...again this should be added to Tips section |
Re: Eliminating Temporary Objects
Code:
Complex x, y, z;Code: CPP
|
Re: Eliminating Temporary Objects
Quote:
Quote:
|
Re: Eliminating Temporary Objects
Quote:
|
Re: Eliminating Temporary Objects
Quote:
Quote:
|
| All times are GMT +5.5. The time now is 14:42. |