Path: utzoo!utgpu!watmath!att!tut.cis.ohio-state.edu!cs.utexas.edu!pp!cadillac!vaughan@mcc.com
From: vaughan@mcc.com (Paul Vaughan)
Newsgroups: comp.lang.c++
Subject: Re: use of new
Message-ID: <2392@cadillac.CAD.MCC.COM>
Date: 17 Aug 89 15:30:29 GMT
References: <2276@cadillac.CAD.MCC.COM> <3614@helios.ee.lbl.gov>
Sender: news@cadillac.CAD.MCC.COM
Reply-To: vaughan@mcc.com (Paul Vaughan)
Organization: MCC VLSI CAD Program
Lines: 85
In-reply-to: beard@ux1.lbl.gov (Patrick C Beard)


   From: beard@ux1.lbl.gov (Patrick C Beard)
   
   In article <2276@cadillac.CAD.MCC.COM> vaughan@puma.cad.mcc.com (Paul Vaughan) writes:
   >
   >	Why is a complete class definition required for the use of the
   >new operator?  Example:
   >---------------------
   >class foo;
   >
   >void bar() {
   >  new foo;
   >}
   >---------------------
   >produces the following error
   >
   >g++ -g -O -fchar-charconst -c -o trash.o trash.cc
   >In function void bar ():
   >trash.cc:4: invalid use of undefined type `struct foo'

   The reason is that new has to know the size of the object being
   allocated.  There is no way to determine this (as far as I know)
   at run time.

I understand that new must know the size of the object, and that new
gets this size from a data structure that only exists at compile time.
What I'm wondering is why it can't be taken care of at compile time.
Consider the following approach:

In the class definition file (foo.cc)
class foo {};

foo* make_foo() {   //not a member function
  return new foo();
}

This is just a simplistic way to alias the constructor.  Now in
another .cc file that makes foo's but doesn't otherwise deal with
properties specific to foo's,

class foo;
extern foo* make_foo();

My first impression is that it would be a bad idea to make the
statement 

class foo; 

automatically declare constructors (and destructors).  On the other
hand, a way to declare external member functions or operators does
make sense.  However, using g++ version 1.35.1, and the following
simple example

class foo;
void foo::bar();

main() {
  foo* f;
  f->bar();
}

I get the following errors.

make decs.o
g++ -g -O  -fchar-charconst -c decs.cc
decs.cc:2: sorry, not implemented: structure `foo' not yet defined
decs.cc:2: no `bar' member function declared in class
In function int main ():
decs.cc:6: cannot lookup method in incomplete type `foo'

The "sorry, not implemented," phrase kind of implies that I might
someday be able to do this.  I can't seem to find anything on this in
Lippman's book.  Does it work with cfront 2.0?  Supposing that this
simple example worked, I suppose the syntax for declaring a
constructor would be

extern foo* foo::operator new();

, but I'm not sure it would be appropriate if the class does not in
fact redefine the new operator.  On another question, is there a way
to get a pointer to a constructor (new operator, whatever) that could
be invoked without having to resort to the "make_foo" approach?

 Paul Vaughan, MCC CAD Program | ARPA: vaughan@mcc.com | Phone: [512] 338-3639
 Box 200195, Austin, TX 78720  | UUCP: ...!cs.utexas.edu!milano!cadillac!vaughan