Member Initializers

+1 Joseph Ville · October 23, 2014
If I have this class declaration in a header file MyClass.h:
MyClass
{
  public:
      MyClass(int x, int y);

  private:
      int h1;
      int h2;
}


Then I use this constructor in a .cpp file, how is using these member initializers different 

MyClass::MyClass(int num1, int num2)
:h1(num1), h2(num2)
{

}


from assigning values to the private variables this way?
MyClass::MyClass(int num1, int num2)
{
   h1 = num1;
   h2 = num2;
}


Do these perform the same action? 
Is there any reason I would want to do it one way versus the other?

Post a Reply

Replies

Oldest  Newest  Rating
0 Joe man · December 1, 2014
Sorry, I still don't get it.

Based on the question Joseph ville asked, is there an advantage for using member initializers over assigning the values? And if there is it most prefereable to use member initializers over assigning values?
0 Joseph Ville · October 28, 2014
Thanks, that really helped a lot!
  • 1

C++

107,075 followers
About

Used in many types of software including music players, video games, and many large scale applications.

Links
Moderators
Bucky Roberts Administrator