: 1) ( ) 2) ( ). , .
-, ++, TU. ( , Go, C.) , forward declarations ( ", " ), . ( , , , .) - , ++.
The second can be mitigated either by a general initialization method, which can be called by any ctor, or by a common base. The latter has other advantages, for example, when member initialization can or should be done in the initialization list. Example:
struct SpanBase {
SpanBase(int start, int stop, int step)
: start(start), stop(stop), step(step)
{
IMAGINE("complex code executed by each Span ctor");
if (start > stop) throw std::logic_error("Span: start exceeds stop");
}
protected:
int start, stop, step;
};
struct Span : protected SpanBase {
Span(int stop) : SpanBase(0, stop, 1) {}
Span(int start, int stop) : SpanBase(start, stop, 1) {}
Span(int start, int stop, int step): StepBase(start, stop, step) {}
};
And finally, C ++ 0x allows you to delegate from one ctor to another, so this whole template is greatly simplified.
Roger Pate
source
share