0

If I initialize member variable a, can I immediately read the value of a to initialize b?

Is this guaranteed by C++(11) specifications to work always with all compilers?

Is there any side effect?

E.g:

class X {
   public:
   int a;
   int b;  
   X() : a(1), b(a+1) {}
};    
robert
  • 1,921
  • 2
  • 17
  • 27
  • 1
    Looks good to me. But note that the order that matters is the order of member variables in the class. `X() : b(1), a(b+1) {}` would not be correct. – john Jul 06 '22 at 15:00
  • 1
    Yes, it's fine, but beware that order in initialization list doesn't matter - members are initialized in order of declaration in class. – Yksisarvinen Jul 06 '22 at 15:01

0 Answers0