Path: utzoo!attcan!uunet!husc6!cs.utexas.edu!pp!riunite!rfg From: rfg@riunite.ACA.MCC.COM (Ron Guilmette) Newsgroups: comp.lang.c++ Subject: Isn't sizeof an operator? Message-ID: <123@riunite.ACA.MCC.COM> Date: 19 Mar 89 19:00:52 GMT Reply-To: rfg@riunite.UUCP (Ron Guilmette) Organization: MCC Austin, Texas Lines: 49 I've already asked some GNU/C++ people about this issue (including Michael Tiemann) but I would like to address the question to a broader audience. I want to create smart objects which can tell me how big they are. Specifically, it would be nice to be able to say: size = sizeof *base_object_pointer; Where base_object_pointer is a pointer to an object either of a base class or some derived class. Obviously, in order to make sizeof return a "correct" value in such cases, the sizeof operator itself would have to be a an explicitly (re-)defined virtual operator for both the base class and for all classes derived from that base class. There are two problems with this. First, neither g++ (1.34.0) nor cfront 1.2 allow "operator sizeof" to be declared. Second, the ideal thing would be to have the compiler automagically provide the (trivial) "function body" for the "virtual operator sizeof" for each class which needed it. That would save a lot of typing. I guess that my question boils down to this. Why is the following (possibly useful) declaration illegal? Must it always be so? ---------------------------------------------------------------------- class sized { public: virtual operator sizeof (); }; ---------------------------------------------------------------------- One final point. It seems to me that one of the basic ideas of object oriented programming is that the programmer should always be able to specify, for any given "type", the set of legal operations for that type. It also seems that "sizeof" breaks this rule, because, for any given "type", the "sizeof" operator is automagically defined, and you cannot either redefine it or undefine it. Perhaps that would not be so bad, if only the sizeof operator always yielded a "correct" value, but in the base/derived example above, "sizeof" can easily be mislead by the "static" type of the object (rather than yielding the sizeof value associated with the "dynamic" type of the object). -- // Ron Guilmette - MCC - Experimental (parallel) Systems Kit Project // 3500 West Balcones Center Drive, Austin, TX 78759 - (512)338-3740 // ARPA: rfg@mcc.com // UUCP: {rutgers,uunet,gatech,ames,pyramid}!cs.utexas.edu!pp!rfg