Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C++ (http://www.go4expert.com/forums/cpp/)
-   -   Eliminating Temporary Objects (http://www.go4expert.com/forums/eliminating-temporary-objects-t820/)

pradeep 22May2006 18:44

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

Complex x, y, z;
 x=y+z; /* temporary created */


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

Complex y,z;
 Complex x=y+z; /* initialization instead of assignment */


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

/* instead of x = y+z; */
 x=y;
 x+=z;


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.

Aztec 22May2006 20:58

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

shabbir 22May2006 23:02

Re: Eliminating Temporary Objects
 
Code:

Complex x, y, z;
x=y+z; /* temporary created */

In the above case you are using the = operator and that needs to be overloaded which should take care of creation of objects. The scenario where the temporary object come into picture are.
Code: CPP

#include <iostream.h>

class A
{
    int i;
public:
    A()
    {
        cout<<"A Constructor"<<endl;
    }
    ~A()
    {
        cout<<"A Destructor"<<endl;
    }
};

A func()
{
    return A();
}

int main()
{
    func();
    cout<<"Now end of main"<<endl;
    return 0;
}


Aztec 23May2006 08:55

Re: Eliminating Temporary Objects
 
Quote:

Originally Posted by shabbir
Code:

Complex x, y, z;
x=y+z; /* temporary created */

In the above case you are using the = operator and that needs to be overloaded which should take care of creation of objects.

That's not necessary as long as you are doing monolithic copying.

Quote:

Originally Posted by shabbir
The scenario where the temporary object come into picture are.
Code: CPP

#include <iostream.h>

class A
{
    int i;
public:
    A()
    {
        cout<<"A Constructor"<<endl;
    }
    ~A()
    {
        cout<<"A Destructor"<<endl;
    }
};

A func()
{
    return A();
}

int main()
{
    func();
    cout<<"Now end of main"<<endl;
    return 0;
}


The code which you have shown I would term it as an advantage of temporary objects.

coderzone 23May2006 10:58

Re: Eliminating Temporary Objects
 
Quote:

Originally Posted by Aztec
The code which you have shown I would term it as an advantage of temporary objects.

Exactly. Its better to be using the temp objects rather than creating a new object and copying the data into it and then returning the object.

shabbir 24May2006 11:17

Re: Eliminating Temporary Objects
 
Quote:

Originally Posted by shabbir
In the above case you are using the = operator and that needs to be overloaded which should take care of creation of objects.

and also the + operator.
Quote:

Originally Posted by Aztec
The code which you have shown I would term it as an advantage of temporary objects.

Yes thats the main reason why we have temp objects after all.


All times are GMT +5.5. The time now is 11:02.