[comp.lang.c++] Calling constructors explicitly

schmidt@siam.ics.uci.edu (Doug Schmidt) (02/24/89)

  In the following short C++ program the constructor for class foo  
is explicitly called as a member function.  However, when compiling
this with cfront 1.2.1 the translator complains that:

----------------------------------------
CC  test.c:
"test.c", line 14: error:  foo is undefined
1 error
----------------------------------------

Apparently, cfront 2.0 also exhibits the same behavior.  Would someone
please tell me whether this is a bug or a feature, i.e., is it part of
the language definition that constructors must not be called
explicitly?

   thank you,
   
       Doug Schmidt                        

----------------------------------------
int printf (char *, ...);

class foo 
{
 public:
  static foo () { printf ("hello world\n"); }
  
};

main () 
{
  foo bar;

  bar.foo ();
}
----------------------------------------
--
schmidt@ics.uci.edu (ARPA) |   Per me si va nella citta' dolente.
office: (714) 856-4043     |   Per me si va nell'eterno dolore.
                           |   Per me si va tra la perduta gente.
                           |   Lasciate ogni speranza o voi ch'entrate.

ark@alice.UUCP (Andrew Koenig) (02/24/89)

In article <8102@paris.ics.uci.edu>, schmidt@siam.ics.uci.edu (Doug Schmidt) writes:

> Apparently, cfront 2.0 also exhibits the same behavior.  Would someone
> please tell me whether this is a bug or a feature, i.e., is it part of
> the language definition that constructors must not be called
> explicitly?

It's a feature.

When I define a constructor, I should be entitled to assume that
the constructor is initializing newly allocated storage -- that is,
that there are no prior values in the object being constructed
that I might have to preserve.

That entitlement vanishes if it is possible to construct a given
object more than once.

Notwithstanding the above, there are arcane circumstances in which
one may want to call a constructor explicitly.  Should one ever be
allowed to do so, it would be in circumstances where the caller would
be expected to take responsibility for doing it only in the case
where the object is not already constructed.  C++ is not a completely
safe language.
-- 
				--Andrew Koenig
				  ark@europa.att.com