Rather than write inextensible constructors taking 12 arguments, I decided to make a new class of "DataPacket" objects which will hold the output of the method. These DataPacket objects are then passed to constructors of the other objects. Now, I came up with two prototypes for DataPacket. They both work fine but strike me messy.
The first is const-correct. It is initialized using a 12-argument constructor and the data it contains is subsequentally accessed using its public methods f1(), etc.
Expand|Select|Wrap|Line Numbers
- class DataPacket
- {
- public:
- DataPacket(const string& field1, const int& field2, ... const int field12)
- : _field1(field1), _field2(field2), ... _field12(field12) {}
- string f1() const { return _field1;}
- int f2() const { return _field2;}
- ...
- double f12() const { return _field12;}
- private:
- const string _field1;
- const int _field2;
- ...
- const double _field12;
- }
- // To make packet:
- DataPacket data("Hello", 12, ..., 5.4351);
- // To read packet:
- string s = data.f1();
- ...
Expand|Select|Wrap|Line Numbers
- class DataPacket
- {
- public:
- string field1;
- int field2;
- ...
- double field12;
- }
- // To make packet:
- DataPacket dp = { "Hello", 12, ..., 5.4351 };
- // To read packet:
- string s = dp.field1
- ...
Passing many arguments to a function must be a common problem, does anybody have nice solutions which work like passing and casting into meaningful types a pointer to void in C, but using proper C++ techniques, passing data-encapsulating objects instead?