Path: utzoo!utgpu!news-server.csri.toronto.edu!bonnie.concordia.ca!uunet!snorkelwacker.mit.edu!bloom-beacon!eru!hagbard!sunic!sics.se!sics!tomas From: tomas@inmic.se (Tomas Lundstrom) Newsgroups: comp.lang.c++ Subject: down cast Message-ID: Date: 8 Apr 91 12:53:54 GMT Sender: news@sics.se Distribution: comp Organization: Swedish Institute of Microelectronics, Kista Lines: 45 A simple question: Assume I have a class hierarchy like this: class base { . . . private: int some_data; public: void some_func (); } class derived : public base { // no more data members . . public: void another_func (); // only a bunch of functions added ... } The derived class introduces NO additional data members. NO virtual functions or multiple inheritance is involved. Now, assume this code fragment including a down cast: base *b = new base; ((derived*)b)->another_func (); // legal and portable ? This is guaranteed to work if an object of class 'base' and 'derived' are of the same size. The question is: is it reasonable to assume that the objects indeed ARE of the same size in all (major) existing implementations ? I want to be sure that this cast works before proceeding in my project. Yes, I know it's ugly, potentially implementation dependent and un-OO, but so is the project I'm in ... :-) :-) thanks in advance, Tomas