Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > own initialization before calling parent's constructor

Reply
Thread Tools

own initialization before calling parent's constructor

 
 
Jeff Flinn
Guest
Posts: n/a
 
      12-08-2011
avasilev wrote:
> Hello,
> The subject says it - is it legal to initialize fields of the derived
> before calling the parent's consctructor, in the initialization list.
> For example:
>
> class Base
> {
> public:
> int b;
> B(int arg):b(arg){};
> };
>
> class Derived: public Base
> {
> int d;
> int calculateValue() {return d+5};
> Derived(int arg):
> d(arg), B(calculateValue()){};
> };
>
> Greetings
> Alex


See the boost base from member idiom at:

http://www.boost.org/doc/libs/1_48_0...om_member.html

Jeff
 
Reply With Quote
 
 
 
 
Goran
Guest
Posts: n/a
 
      12-09-2011
On Dec 8, 8:34*pm, avasilev <(E-Mail Removed)> wrote:
> > Really, is this a joke? Just calculate your value before going to a
> > constructor. It then becomes

>
> > Derived(int calculatedValue)

>
> calculatedValue is an internal stuff that does not concern the outer
> world, so passing as constructor argument is out of the question.


Then you can use a static function to create your object and use value
returned from veryLongCalculation there.

>
> > * : d(calculatedValue)
> > * , Base(calculatedValue)
> > {

>
> > }

>
> And then we are where we started - initializing derived member d,
> before calling base constructor


Not true. Statement order in initialization list is not execution
order. It can't be. Base classes are construted first irregardless of
this order.

Think about it: if you could do it, then you could construct your own
members before base was constructed. If that could happen, then, upon
an eception in initialization list, destructor wouldn't know what to
do. Imagine:

Derived(params1)
: d(...)
, Base(...)
{
}

Derived(params2)
: Base(...)
, d(...)
{
}

Destruction for exceptions case order would have to be different for
two cases, otherwise an attempt to destroy non-constructed object
could be made.

Goran.
 
Reply With Quote
 
 
 
 
Stuart Redmann
Guest
Posts: n/a
 
      12-09-2011
On Dec 8, avasilev wrote:
> > [snip] I would like to
> > do some caching of calculated result, which I then use to pass to the
> > base's constructor, and will use further in the object's lifetime for
> > other purposes. Normally I would store the result in a variable, and
> > not initialize any members, but given the limited syntax of
> > initializer lists, I cannot implement logic that I would do normal
> > code. Let me clarify with another example:
> >
> > struct Base
> > {
> > Base(int v) {...};
> >
> > }
> >
> > struct Derived: public Base
> > {
> > int d;
> > void veryCostlyCalculation()
> > {
> > ....
> > d = <calculation result>
> > }
> >
> > Derived()
> > :veryCostlyCalculation(), Base(d) {}
> >
> > };
> >
> > In an initialization list, how can I store the result of
> > veryCostlyCalculation() in a local variable, so that I don't use
> > members?



On 8 Dez., Gert-Jan de Vos wrote:
> This works but isn't pretty:
>
> #include <iostream>
>
> struct Base
> {
> Base(int v) : dBase(v) { }
>
> int dBase;
>
> };
>
> struct Derived : public Base
> {
> Derived(int cachedValue = 0) :
> Base(cachedValue = veryCostlyCalculation()),
> dDerived(cachedValue)
> {
> }
>
> static int veryCostlyCalculation()
> {
> std::cout << "veryCostlyCalculation\n";
> return 12;
> }
>
> int dDerived;
>
> };
>
> int main()
> {
> Derived d;
>
> std::cout << d.dBase << "\n";
> std::cout << d.dDerived << "\n";
>
> }


Not pretty? It's ingenious!

Regards,
Stuart
 
Reply With Quote
 
avasilev
Guest
Posts: n/a
 
      12-09-2011
> > And then we are where we started - initializing derived member d,
> > before calling base constructor

>
> Not true. Statement order in initialization list is not execution
> order. It can't be. Base classes are construted first irregardless of
> this order.
>

I agree, was too tired. Your approach of course works, as well as the
others proposed.
Just as a note - my point was not if it is at all possible to do, but
if there is a simple trick to do it, with minimum fuss and changes,
because it is a small optimization that is not worth changing the
design. The thing I didn't know is that the constructor call order is
actually part of the member-init-order-as-declared rule, and base
subobjects are regarded as members in this context.
Thanks to all for the replies
Alex
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
help on constructor/constructor initialization list with inheritanceusing templates balaji C++ 3 08-10-2011 12:06 PM
A constructor calling another constructor (default constructor)? Generic Usenet Account C++ 10 11-28-2007 04:12 AM
Calling own-constructors inside another constructor EdUarDo Java 4 12-21-2005 12:23 PM
doing some initialization before the constructor gets called Vamsi C++ 3 07-19-2005 04:46 AM
Server Side button calling page_load before calling it's own click event. Ryan Ternier ASP .Net 4 07-29-2004 01:06 PM



Advertisments