I have a method which needs to pass some data to the constructors of two other objects. The data is of different types, like a string, an int, etc. I have 12 fields in total.
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.
[code=cpp]
class DataPacket
{
public:
DataPacket(cons t string& field1, const int& field2, ... const int field12)
: _field1(field1) , _field2(field2) , ... _field12(field1 2) {}
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();
...
[/code]
The second is basically a struct. It members are explicitly set and can be subsequently accessed directly, but they can also be changed after its inception which is not desired behaviour.
[code=cpp]
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
...
[/code]
I like the simplicity of the second but not the fact that its contents can be later modified. This is prevented in the first version, but that contains a number of trivial functions which strike me as reduntant and I don't like its 12-argument constructor.
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?
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.
[code=cpp]
class DataPacket
{
public:
DataPacket(cons t string& field1, const int& field2, ... const int field12)
: _field1(field1) , _field2(field2) , ... _field12(field1 2) {}
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();
...
[/code]
The second is basically a struct. It members are explicitly set and can be subsequently accessed directly, but they can also be changed after its inception which is not desired behaviour.
[code=cpp]
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
...
[/code]
I like the simplicity of the second but not the fact that its contents can be later modified. This is prevented in the first version, but that contains a number of trivial functions which strike me as reduntant and I don't like its 12-argument constructor.
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?
Comment