Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP
Path: utzoo!mnetor!seismo!mcvax!inria!shapiro
From: shapiro@inria.UUCP (Marc Shapiro)
Newsgroups: comp.lang.c++
Subject: explicit initializations
Message-ID: <340@inria.UUCP>
Date: Fri, 12-Dec-86 09:03:52 EST
Article-I.D.: inria.340
Posted: Fri Dec 12 09:03:52 1986
Date-Received: Mon, 15-Dec-86 05:48:01 EST
Organization: INRIA, Rocquencourt. France
Lines: 69

Several messages have asked for a syntax for calling constructors
explicitly.  The official answer has been that it's not a good idea; that
it's better to provide an initialization procedure and call that procedure
explicitly.  I don't like that answer: I remember reading in some
introductory paper on C++ that one of the advantages of C++ over C is that
initialization is automatic, whenever a constructor is provided, and I
believed it.

An other answer is to use derived classes: put the necessary
initializations in a derived class.  This is not always appropriate since
the derived constructor is always called after the base constructor.  Here
is an example: suppose you want to dedicate a task to each instance.  You
spawn a task in the base constructor.  You lose, because the derived
constructor is executed in parallel with the newly-spawned task intstead
of being done within it.  In our project we have many other examples.

I had thought of the following workaround: all my classes are derived from
some base class.  This base class declares a private, virtual initializer
"initialize()".  The constructor for the base class calls "initialize()"
which is re-declared in all the subclasses to do whatever is needed.
Unluckily, this does not work: even though "initialize()" is a virtual, it
is always base::initialize which is called, as demonstrated by the
following program:

int printf (char* ...);

class base {
   virtual int sz() {return sizeof (*this);};
   virtual void initialize() {i=sz();};
 public:
   int i;
   int size () {return sz();};
   base () { initialize();};
};

class derived: public base {
    virtual int sz() {return sizeof (*this);};
    char* s;
 public:
    int j;
    derived (char* ss) {j=sz(); s=ss;};
};

void main (int argc, char *argv[]) {
    base b;
    derived d ("zzz");
    printf ("sizeof b = %2d, b.size() = %2d, b.i = %2d\n",
             sizeof b,       b.size(),       b.i);
    printf ("sizeof d = %2d, d.size() = %2d, d.i = %2d, d.j = %2d\n",
             sizeof d,       d.size(),       d.i,       d.j);
    }

which prints the following results (on a Vax):

sizeof b =  8, b.size() =  8, b.i =  8
sizeof d = 16, d.size() = 16, d.i =  8, d.j = 16


I find it odd that the call to virtual 'sz' has a different semantics
within the constructor for base (call base::sz) and within
procedure 'size' (call this->sz).  Is this a bug or a feature?

Conclusion: there exists a need for a syntax to express explicit
initialization of an instance.  This need would be filled by a syntax to
get the address of the constructor.  Such an addition would leave room for
the future enhancement of dynamic linking (that's why my group needs it).


				Marc