"initialization lists" Vs "assignment"

Discussion in 'C' started by asadullah.ansari, Mar 12, 2008.

  1. asadullah.ansari

    asadullah.ansari TechCake

    Joined:
    Jan 9, 2008
    Messages:
    356
    Likes Received:
    14
    Trophy Points:
    0
    Occupation:
    Developer
    Location:
    NOIDA
    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 ?
     
  2. heena.mca

    heena.mca New Member

    Joined:
    Feb 14, 2008
    Messages:
    20
    Likes Received:
    0
    Trophy Points:
    0
    code will run faster if you use initialization lists rather than assignment.
     
  3. asadullah.ansari

    asadullah.ansari TechCake

    Joined:
    Jan 9, 2008
    Messages:
    356
    Likes Received:
    14
    Trophy Points:
    0
    Occupation:
    Developer
    Location:
    NOIDA
    Due to both my statement, code will run faster. means you are telling only results of my two statement.
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice