Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c++ > #12202
| From | Victor Bazarov <v.bazarov@comcast.invalid> |
|---|---|
| Newsgroups | comp.lang.c++ |
| Subject | Re: own initialization before calling parent's constructor |
| Date | 2011-12-08 13:55 -0500 |
| Organization | A noiseless patient Spider |
| Message-ID | <jbr16d$31h$1@dont-email.me> (permalink) |
| References | <ebf5a2e8-c94a-4380-b21f-045cf5314fa5@j10g2000vbe.googlegroups.com> <jbqjv4$7h4$1@localhost.localdomain> <cf59fb10-ec26-48a9-b0cb-ecd5e41c4f17@d10g2000vbf.googlegroups.com> <43399a83-e5b5-4874-8f87-84bc4b294c81@f30g2000pri.googlegroups.com> <27c67c67-20a6-495c-af52-b7cc5b87cf65@e2g2000vbb.googlegroups.com> |
On 12/8/2011 1:40 PM, avasilev wrote:
> 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.
You could also try using the default argument mechanism:
class Derived : public Base
{
...
explicit Derived(int a = costlyCalculation()) : Base(a) {}
};
V
--
I do not respond to top-posted replies, please don't ask
Back to comp.lang.c++ | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
own initialization before calling parent's constructor avasilev <alxvasilev@gmail.com> - 2011-12-08 05:07 -0800
Re: own initialization before calling parent's constructor Victor Bazarov <v.bazarov@comcast.invalid> - 2011-12-08 09:03 -0500
Re: own initialization before calling parent's constructor avasilev <alxvasilev@gmail.com> - 2011-12-08 08:12 -0800
Re: own initialization before calling parent's constructor Victor Bazarov <v.bazarov@comcast.invalid> - 2011-12-08 12:04 -0500
Re: own initialization before calling parent's constructor Bart v Ingen Schenau <bart@ingen.ddns.info> - 2011-12-08 16:09 +0100
Re: own initialization before calling parent's constructor avasilev <alxvasilev@gmail.com> - 2011-12-08 08:10 -0800
Re: own initialization before calling parent's constructor Victor Bazarov <v.bazarov@comcast.invalid> - 2011-12-08 12:05 -0500
Re: own initialization before calling parent's constructor avasilev <alxvasilev@gmail.com> - 2011-12-08 10:22 -0800
Re: own initialization before calling parent's constructor Victor Bazarov <v.bazarov@comcast.invalid> - 2011-12-08 13:44 -0500
Re: own initialization before calling parent's constructor Gert-Jan de Vos <gert-jan.de.vos@onsneteindhoven.nl> - 2011-12-08 10:51 -0800
Re: own initialization before calling parent's constructor Victor Bazarov <v.bazarov@comcast.invalid> - 2011-12-08 14:04 -0500
Re: own initialization before calling parent's constructor avasilev <alxvasilev@gmail.com> - 2011-12-08 11:07 -0800
Re: own initialization before calling parent's constructor Stuart Redmann <DerTopper@web.de> - 2011-12-09 05:43 -0800
Re: own initialization before calling parent's constructor Goran <goran.pusic@gmail.com> - 2011-12-08 11:09 -0800
Re: own initialization before calling parent's constructor avasilev <alxvasilev@gmail.com> - 2011-12-08 11:34 -0800
Re: own initialization before calling parent's constructor Victor Bazarov <v.bazarov@comcast.invalid> - 2011-12-08 15:15 -0500
Re: own initialization before calling parent's constructor Goran <goran.pusic@gmail.com> - 2011-12-09 00:07 -0800
Re: own initialization before calling parent's constructor avasilev <alxvasilev@gmail.com> - 2011-12-09 05:54 -0800
Re: own initialization before calling parent's constructor Kevin McCarty <kmccarty@gmail.com> - 2011-12-08 09:27 -0800
Re: own initialization before calling parent's constructor avasilev <alxvasilev@gmail.com> - 2011-12-08 10:40 -0800
Re: own initialization before calling parent's constructor Victor Bazarov <v.bazarov@comcast.invalid> - 2011-12-08 13:55 -0500
Re: own initialization before calling parent's constructor Kevin McCarty <kmccarty@gmail.com> - 2011-12-08 12:41 -0800
Re: own initialization before calling parent's constructor jgk@panix.com (Joe keane) - 2011-12-08 21:20 +0000
Re: own initialization before calling parent's constructor Paavo Helde <myfirstname@osa.pri.ee> - 2011-12-08 11:17 -0600
Re: own initialization before calling parent's constructor Jeff Flinn <TriumphSprint2000@hotmail.com> - 2011-12-08 16:42 -0500
csiph-web