0

So I've seen two different ways of initialising an object's member variable in the constructor:

MyObject(int var1, int var2) :
    mMemberOne(var1),
    mMemberTwo(var2)
{

}

and (the way I've always done it previously):

MyObject(int var1, int var2)
{
    mMemberOne = var1;
    mMemberTwo = var2;
}

Is there any benefit of one over the other? I ask on here because I don't know the correct terms to search for with Google. I just get pages about how to initialise an object or other basic principles.

Thanks

Luke
  • 560
  • 6
  • 26
  • 1
    The first one creates the members initalised to the values you passed, the second initialises your members possibly to garbage and then assigns the value, the first method is more efficient and should be the convention – EdChum Oct 06 '14 at 15:11
  • 1
    `initialization-lists` is the keyword you should look for. – Patrick B. Oct 06 '14 at 15:12
  • Also related: http://stackoverflow.com/questions/7350155/initialisation-and-assignment – EdChum Oct 06 '14 at 15:13
  • and this: http://www.parashift.com/c++-faq/init-lists.html – EdChum Oct 06 '14 at 15:13
  • And http://stackoverflow.com/questions/4589237/in-this-specific-case-is-there-a-difference-between-using-a-member-initializer – Mat Oct 06 '14 at 15:13

0 Answers0