If all the elements of an object are constants at compile time, can C++ build them in the text segment (eg flash or progmem on Arduino) instead of using a constructor routine at runtime to build them in RAM ?
(theoretically? If that's different from the actual answer...)
If the SerialN objects use some of the same constant these should be static const, or maybe static const PROGMEM (or a variant of that, don't know if static and PROGMEM works together, seems a bit redundant...) if that works. :]
When trying to decide whether to store an unchanging value in RAM or in PROGMEM, consider this: if you store something in PROGMEM space, when you go to retrieve it its PROGMEM offset/address must be available in RAM!
This means that it is really only worthwhile to store biggish objects/variables in PROGMEM, because there is always a small overhead, both in terms of storage and runtime efficiency, in accessing PROGMEM.
If you wanted, you could define a structure that collected all those buffer and register pointers and masks together. Then your constructor use would look something like
This might save a little RAM, but at the expense of having to load each of these values from PROGMEM whenever they were needed.
My NewSoftSerial library uses an embedded "static const PROGMEM" lookup table, but this is a fairly large object.
static and PROGMEM may seem similar, but they have markedly different meanings. The static keyword tells as much about an object's visibility as it does dictate where it is stored.
In NewSoftSerial.cpp, for example, I use static to indicate that I do not want the moniker "table" to be visible external to the module itself.
I believe templates give you almost what you want. I've expermented a bit with templates and a simple LED class. The results were not what I expected. The code was smaller and probably a bit faster to use a "normal" class instead of template. Basically, the compiler did a very good job of register allocation and, once a value (like a pointer to "this") was in a register, the instructions necessary to use the value were about the same as when the value was a constant.