vaughan@puma.cad.mcc.com (Paul Vaughan) (08/17/89)
Why is a complete class definition required for the use of the
new operator? Example:
source file trash.cc
---------------------
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'
I have classes that have pointers to their components (class
objects) rather than having their components included directly as data
members. This allows me to swap out the components with other
(derived) versions of the components, either at run or compile time.
For instance, I occassionally swap out a silent component for one that
prints whenever anything happens to it for debugging purposes. The
printing version just redefines some of the virtual functions of the
base. Everything works fine, but I wish I didn't have to include the
entire class definition in any file that might make a printing
version. I don't understand the issues involved here, especially when
the new operator can be overloaded. I'm just wondering if anybody
thought much about this.
Yes, my compile times are getting long enough to make my mind
wander onto how to shorten them.
Paul Vaughan, MCC CAD Program | ARPA: vaughan@mcc.com | Phone: [512] 338-3639
Box 200195, Austin, TX 78720 | UUCP: ...!cs.utexas.edu!milano!cadillac!vaughanbeard@ux1.lbl.gov (Patrick C Beard) (08/17/89)
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. To see why this is the case, here is the definition of new as an operator: void* operator new(size_t size) { return (void*)malloc(size); } So, if somebody comes up with a way to compute the size of an object at runtime then we will be able to do what you are asking. ------------------------------------------------------------------------------- - Patrick Beard, Macintosh Programmer (beard@lbl.gov) - - Berkeley Systems, Inc. ".......<dead air>.......Good day!" - Paul Harvey - -------------------------------------------------------------------------------
vaughan@mcc.com (Paul Vaughan) (08/17/89)
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
ark@alice.UUCP (Andrew Koenig) (08/17/89)
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; > } If the compiler hasn't seen the class definition for foo, it doesn't know if `new foo' is legal. The foo class might not have an empty constructor defined... -- --Andrew Koenig ark@europa.att.com