8

Possible Duplicate:
Constructor initialization-list evaluation order

While writing a C++ constructor for a class, why is it that the order of initialising the member fields should be the order in which they are declared?

Consider the following C++ code. On compiling with gcc (gcc -g -Wall foo.cpp) I get the Warning

g++ -g -Wall main.cpp 
main.cpp: In constructor ‘myclass::myclass(int, int, int, int, int, int)’:
main.cpp:12: warning: ‘myclass::z’ will be initialized after
main.cpp:11: warning:   ‘int myclass::y’
main.cpp:26: warning:   when initialized here

Here is the code. In this, the member z appears in the initialization list of the constructor class before y and throws the above warning.

#include <iostream>
#include <iomanip>

class myclass
{
public:
  int x;
  int y;
  int z;
  myclass(int num1, int num2, int num3, int num4, int num5, int num6);//constructor for the class

private:
  int a;
  int b;
  int c;
};

myclass::myclass(int num1, int num2, int num3, int num4, int num5, int num6) 
  :x(num1),z(num3),  y(num2), a(num4),b(num5),c(num6)
{}

int main(int argc, char *argv[])
{
  myclass jimmy(23,34,56,67,89,91);

  std::cout << jimmy.x << std::endl;
  std::cout << jimmy.y << std::endl;
  std::cout << jimmy.z << std::endl;

  return 0;
}
Community
  • 1
  • 1
curiousexplorer
  • 1,217
  • 1
  • 17
  • 24
  • 1
    100% dupe, lemme find it... to answer the question, because the compiler will always initialize them in the order they are defined inside the class, no matter how you write your mem-initializer. – Xeo Feb 29 '12 at 20:46
  • 1
    The warning is probably there to remind you that the list isn't the place where the order is set, in case you were hoping to achieve something critical by changing the initialization order. – tmpearce Feb 29 '12 at 20:50

1 Answers1

9

This may help,

Constructor initialization-list evaluation order

Please see AProgrammer's reply in the above,

"The reason for which they are constructed in the member declaration order and not in the order in the constructor is that one may have several constructors, but there is only one destructor. And the destructor destroy the members in the reserse order of construction." – AProgrammer Aug 7 '09 at 6:45

Community
  • 1
  • 1
ehuffman
  • 141
  • 3
  • +1 Beat me to it, and had a better reference to top it off :) – Joachim Isaksson Feb 29 '12 at 20:57
  • I see that you are a newcomer at SO (StackOverflow), so welcome and congrats on your first participation. However, you should know that the answer you gave is not really a good fit for this site: there is a mechanism for linking duplicate questions, so there is no need to link and copy the content of another answer. If you want to give a link to a similar question, you should do so in a comment to the question (you'll be able to comment soon, when you'll have enough reputation). Later, you'll find that you can mark a question as duplicate (as is now the case for this one). – Luc Touraille Feb 29 '12 at 22:35
  • Luc, thank you for both the welcome and direction, greatly appreciated. I'll make sure to look for duplicates and use the mechanism you reference in the future. Thanks again. – ehuffman Feb 29 '12 at 23:02