Path: utzoo!attcan!utgpu!jarvis.csri.toronto.edu!rutgers!mailrus!csd4.milw.wisc.edu!srcsip!nic.MR.NET!ns!hughes From: hughes@ns.network.com (Jim Hughes x1676) Newsgroups: comp.lang.c++ Subject: Re: sizeof in class declaration Keywords: sizeof declaration Message-ID: <1488@ns.network.com> Date: 15 Jul 89 17:12:54 GMT References: <533@atcmpe.atcmp.nl> Sender: hughes@ns.network.com (Jim Hughes x1676) Reply-To: hughes@ns.UUCP (Jim Hughes x1676) Organization: Network Systems Corporation Lines: 34 In article <533@atcmpe.atcmp.nl> leo@atcmp.nl (Leo Willems) writes: >This is a question regarding sizeof in a class declaration. >I use the sizeof operator inside a class declaration: > >This example does compile with -our- compiler. But is it legal to take the >size of an object in the objects declaration? > >Leo Willems Internet: leo@atcmp.nl Not only is it legal, but it even works on AT&T 1.2. (Andrew, does it work on 2.0 :-) The following program prints the correct answer of 4. Another way of making it work would be to define the function as inline outside of the class definition. Your other question about putting a p inside of p is also illegal because it would be infinitely recursive. //test.c #include class p{ public: int i; p(){ i = sizeof(p); } // is this legal ?? }; main() { p p1; printf("%p.i=%d\n",p1.i); } jim hughes@network.com