From mboxrd@z Thu Jan 1 00:00:00 1970 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 10f8a1,e7e13c943c611b62 X-Google-Attributes: gid10f8a1,public X-Google-Thread: 102b75,be3d6a6131c61fa7 X-Google-Attributes: gid102b75,public X-Google-Thread: 1164f8,be3d6a6131c61fa7 X-Google-Attributes: gid1164f8,public X-Google-Thread: 109fba,d7602446ac50f05e,start X-Google-Attributes: gid109fba,public From: Erik Corry Subject: Fragile Base Class Date: 1999/05/06 Message-ID: <7gsbfp$25ej3$1@xinwen.daimi.au.dk>#1/1 X-Deja-AN: 474768817 References: <36E9F2A0.9A401A09@pacific.net.sg> <924046418.053.46@news.remarQ.com> <7fv0ib$1kgem$1@xinwen.daimi.au.dk> <373197F3.BA3DC720@cs.city.ac.uk> Followup-To: comp.lang.c++ X-Complaints-To: news@daimi.au.dk X-Trace: xinwen.daimi.au.dk 926003513 2275939 255.255.255.255 (6 May 1999 15:11:53 GMT) Organization: University of Aarhus, Department of Computer Science (DAIMI) User-Agent: tin/pre-1.4-980818 ("Laura") (UNIX) (SunOS/5.7 (sun4m)) NNTP-Posting-Date: 6 May 1999 15:11:53 GMT Newsgroups: comp.arch,comp.sys.dec,vmsnet.alpha,comp.lang.c++ In comp.arch Greg Law wrote: > Erik Corry wrote: >> >> In comp.arch Zalman Stern wrote: >> >> > There is definitely room for improvement in binary interoperability in C++ >> > environments. >> >> The problems are not restricted to differences between C++ >> compilers and versions, but are also made worse by the normal >> recommended C++ programming idiom where the sizes of all >> objects are exposed through the .h files to the users of your >> libraries who will put them on the stack, make arrays of them, >> subclass them and generally do things that mean you can never >> ever add as much as one single private member variable without >> breaking binary compatibility with your old library versions. >> >> There are ways around this 'fragile base class' problem, of >> course, where you put all the data in a separate object, or >> make sure you have enough padding in your classes for future >> expansion, but C++ doesn't actively support this way of doing >> things, or check that you don't accidentally get it wrong. >> >> Qt and Be's libraries suffer from this. I imagine any C++-based >> library will. > But without separating implementation from interface, how do you do > this? You don't. > Sure, such separation is generally a good thing, only it costs. Virtual methods are also a good thing, but they cost, so C++ made them optional, so perhaps it should be an optional feature of a class. Something like class interface C { ... } which signals to the compiler that this class is to be used in the externally visible interface in a DLL and binary compatibility across versions is desired. That probably means moving all private data members into a separate autoallocated indirected area and prohibiting public and protected data members. Then you would issue errors for inlined and non-virtual methods, and use a hashed lookup for virtual methods to make sure the right one gets called even if a later implementation reorders or adds methods (even adding is dangerous if the class is subclassed with the present system). Perhaps the run-time linker can do something clever and efficient instead here. Even a more minimal feature like: class padded 256 vmt 32 C { ... } that automatically padded my sizeof up to 256, padded the virtual method table to 32 and warned me if I exceeded those limits would make the pad-your-objects-presciently method more manageable without having to do horrendous manipulations with sizeof and macros. > C++ classes can efficiently represent quite small things - when you add That's great, but not usually for the interface of a DLL. > the level of indirection necessary to separate interface from > implementation then you get "proper OO", but you need coarse granularity > of objects to run at the same speed. C++ was never intended to do what > you're asking of it - if you want that, use another language. What you are saying is that C++ is unsuitable for implementing DLLs or shared libraries. That's almost true, but that's a huge area to cut it off from. This isn't some niche application, shared libraries are everywhere. Breaking each time a private data member is added is not an option. > If you want speed, use C++ Ah, but what if I want speed, robustness and O-O in my shared library/DLL? Is C++ the wrong language, or do I have to be incredibly disciplined and foresightful and cannot rely on the compiler telling me when I screw up. -- Erik Corry erik@arbat.com