Hi Kevin,
Thanks for the detailed response.
Then your code is simply not going to work. That's just how the
language is. The derived class instance (and any non-static members
of it) does not yet exist at the time the base class constructor is
invoked
I think that says it all! That was also my doubt, and the reason to
ask.
You have a few alternatives for a work around, all of which have their
ups and downs:
1) Use a static member function of the derived class for the
calculation
This will not do - actually my purpose is to cache the result from the
calculation for later reuse, but in an initializer list I can't have
local variables, this is why I wanted to use a member variable for
this purpose.
2) Use two-phase initialization for the base object
3) Multiple inheritance: move the calculation logic into another base
class, and rely on the fact that base class constructors are called in
the order in which they are inherited from.
These will technically do the job, but my optimization is too small to
resort to complicating the design in such ways. I guess I will just
leave the optimization for later.
...