Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/forums/c/)
-   -   "initialization lists" Vs "assignment" (http://www.go4expert.com/forums/initialization-lists-vs-assignment-t9269/)

asadullah.ansari 12Mar2008 14:30

"initialization lists" Vs "assignment"
 
Can you people give me some more than two reasons why constructors should use Initialization lists or Assignment operator ?
Two reasons are
ex. class MyTest;

Use
MyTest :: MyTest(): obj(p)
{
;
}
Instead Of
MyTest :: MyTest()
{
Obj=p;
}

1. In assignment operator case, one extra by default ctor will be called.
2. In Assignment operator case, a temporary variable is created and assigned to p and
then that temporary variable is passed to obj. Means extra temporary variable
creation in case of assignment operator.


Can you give me other reason except these two? This is question asked to me some C++ comptetion ?

heena.mca 13Mar2008 15:52

Re: "initialization lists" Vs "assignment"
 
code will run faster if you use initialization lists rather than assignment.

asadullah.ansari 13Mar2008 16:53

Re: "initialization lists" Vs "assignment"
 
Quote:

Originally Posted by heena.mca
code will run faster if you use initialization lists rather than assignment.

Due to both my statement, code will run faster. means you are telling only results of my two statement.


All times are GMT +5.5. The time now is 00:15.