Yes, it is safe, but in this case you have to be careful. You are using var , not _var , so you must be sure that var built before i . This is the case here because the members are built in the order of their declaration (in this case var , i ), which may differ from the order in which they appear in the initializer list.
So in this case it works. It will also work in this case:
C(int _var): i(var*var), var(_var)
but not this:
class C{ public: C(int _var): var(_var), i(var*var) {} private: int i; int var; };
But of course, to be always safe, you could just use _var :
C(int _var): var(_var), i(_var*_var)
Christian rau
source share