View Single Post
  #4   Spotlight this post!  
Unread 24-10-2013, 00:38
William Kunkel William Kunkel is offline
Programming Lead
AKA: Kunkel
FRC #0422 (Mech Tech Dragons)
Team Role: Programmer
 
Join Date: Jan 2013
Rookie Year: 2011
Location: Richmond, VA
Posts: 94
William Kunkel is an unknown quantity at this point
Re: Same initialization as declaration in constructor, why?

The reason for this is that if you want to declare a member variable as some transformation of another member variable, you can end up with declaring it before the first variable is initialized. So, if you have:
Code:
class Example {
private:
    int a;
    int b;
public:
    Example();
};

Example::Example():
    b(2), a(b+1) {
}
the value of a will not be 3, but undefined, even though it looks like it's declared after b. It's not a compiler error to have them in a different order, but it will produce a warning (I believe the warning is "warning: when initialized here").
Reply With Quote