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) {} 

Popular posts from this blog

php - How should I create my API for mobile applications (Needs Authentication) -

5 Reasons to Blog Anonymously (and 5 Reasons Not To)

Google AdWords and AdSense - A Dynamic Small Business Marketing Duo