c++ - Intermediate calculations inside initialization list -
i have like
struct foo { const double a; const double b; foo(double c); } foo::foo(double c) { double tmp = f(c); = g(tmp); b = h(tmp); }
where f,g,h
functions implemented elsewhere. gives expected "uninitialized const member" error.
i fix with
foo::foo(double c): (g(f(c))), b (h(f(c))) {}
but f
expensive function , wouldn't run twice.
my question is, how can solve problem without running f
twice or making tmp
permanent member of foo
?
typically, delegating constructors offer simple solution type of problem. in case you'll have introduce way distinguish between 2 constructors:
private: // int param unused foo(double fc, int) : a(g(fc)), b(h(fc)) {} public: foo(double c) : foo(f(c), 0) {}