Is it stable to use something like this in a class’s ctor initialization list during implicit assignment (no operators are overloaded):
class C{
public:
C(int _var): var(_var), i(var*var)
{}
private:
int var;
int i;
};
I’m getting some eratic results, why is this?
Yes.
You might want to get rid of the initialization order dependency, and write:
Basically, by making i depend on var, you must ensure that var is declared before i in the class.
Similarly, you can initialize something in C that is defined (and initialized) in a parent class, because the parent will be constructed before C.
Best practices dictate that you be aware of the above, and avoid situations that obfuscate any of that – maybe document i’s dependence on var, so that the next programmer (maybe yourself) doesn’t introduce an initialization order issue.