From mboxrd@z Thu Jan 1 00:00:00 1970 From: "Kenneth Porter" To: "c++-embedded@cygnus.com" Subject: Re: biggest deterrant to using C++? Date: Mon, 24 Aug 1998 18:39:00 -0000 Message-id: <199808250049.UAA22357@mail1.ispnews.com> X-SW-Source: 1998/msg00027.html On Mon, 24 Aug 1998 03:37:50 +0200, Michael Bruck wrote: >const atom_bomb my_bomb(50, 100); > >I would like to have my_bomb somewhere in the ROM. And the example above >should not generate any code as long as I don't call the constructors in >another context. What happens is, that the compiler always generates >a function for each source file that calls all the constructors and >then generates a section that contains the pointer to that function. >These sections from all files are then linked together into a table of >function pointers and the init-functions of all global objects are >called at program startup through this table. This is ok if my >constructors are doing something other than just copying around values. >This would cause in most situations just some overhead, >but if you want to use ROM it's impossible to use (virtual) classes. I doubt we'll see compilers soon that are clever enough to statically init an object with a vpointer and only scalar initializers in the ctor. 'Twould be nice, though! I recall looking up this question a few weeks ago (can't remember where) and I recall reading that the proper way to do this was to put things with static data in ROM'd POD structs and including an instance or reference to the struct in a small RAM-based class object. To hide the POD internal structure, declare it within the class: class Thing { struct ThingROMData { const int weight; }; const ThingROMData& data; Thing(const ThingROMData& _data) : data(_data) {}; public: static void InitThings(); // calls special ctor virtual bool get(); }; static const Thing::ThingROMData thing1data = { 100 }; // this gets ROM'd The actual machinery of Thing initialization will be application-specific, but this gives the basic idea of how to structure the ROM part of the data. In your example, since you want to parameterize the ctor, I'd suggest using a macro to perform the parameterization (since macros are expanded at compile time). I was going to suggest a template, but I can't see how to use a template to instantiate a class instead of creating a new type. Kenneth Porter Systems Engineer Kensington Laboratories, Inc. 750 National Court Richmond, CA 94804-2008 Voice: 510-620-0235 FAX: 510-233-5544 mailto:kenneth_porter@kensingtonlabs.com http://www.kensingtonlabs.com