-
August 4th, 2014, 10:00 PM
#1
Constructor with lots of parameters
Hi
I have class that have member variables which is also a class object that contains member variables which is also a class... so on and so on... like this
Code:
class Device
{
public:
Device();
...
private:
QScopedPointer<Data> _data;
double _value;
};
class Data
{
....
private:
QScapedPointer<Record> _rec;
int _nType;
double _nTest;
};
class Record
{
...
private:
QScopedPointer<Players> _players;
int _nNumberRecord;
....
};
class Players
{
private:
int _nCountPlayers;
....
};
When i create a device object, i want to be able to initialize all its member variables, such _data, _value, but since Data has also a set of members need to be initialized, i also need to pass parameter like Record, Type, Test etc to Device constructor, all down to the primitives level of member variables of all its embedded classes object... the top level class constructor may need a very very long list of parameters....depending on the number of member variables each embedded class contains! I know i can break down the initialization process, but i prefer to do a one line initialization, without much objects copying etc... and i use scoped pointer where it need to be initialized just once. Is it common to code a constructor with long list of parameters??
Code:
Device::Device(double deviceValue, int countPlayers, int NumRecord, int DataType, double dataTest)
{
_data = QScopedPointer<Data> (new Data(NumRecord, countPlayers, dataType, dataTest));
_value = deviceValue;
}
Data::Data(int nRecord, int nCountPlayers, int dataType, double dataTest)
{
_rec = QScopedPointer<Record>(new Record(.......);
_nTest = dataTest;
_nType = dataType;
}
Record::Record(.....)
{
}
main
{
Device dev = new Device(.................................................)
}
-
August 5th, 2014, 12:11 AM
#2
Re: Constructor with lots of parameters
Considering the fact that you store a QScopedPointer<Data> object rather than a Data object as a member in Device makes me think that Data is an abstract base class. As such, you should not be imposing a concrete interface in the Device constructor that translates a particular Device subclass constructor's interface into the interface of a Data constructor. Rather, have the user pass a (smart) pointer to Data in the Device constructor such that the member QScopedPointer takes ownership of the Data object, whatever the Data subclass.
-
August 5th, 2014, 12:51 AM
#3
Re: Constructor with lots of parameters
have the user pass a (smart) pointer to Data in the Device constructor such that the member QScopedPointer takes ownership of the Data object, whatever the Data subclass.
What smart pointer to pass in the ctor so that the member QScopedPointer<Data> can take over ownership? I mean do you mind to show me how to correctly release the ownership to member class _data in Device ctor?
Do you means this
Device:evice(QScopedPointer<Data> data, ....) : _data(data.take())
{
}
-
August 5th, 2014, 02:22 AM
#4
Re: Constructor with lots of parameters
Originally Posted by mce
What smart pointer to pass in the ctor so that the member QScopedPointer<Data> can take over ownership? I mean do you mind to show me how to correctly release the ownership to member class _data in Device ctor?
Do you means this
Code:
Device::Device(QScopedPointer<Data> data, ....) : _data(data.take())
{
}
I think that that will work: the call to take() should be effectively giving you move semantics, which is desirable in this case.
-
August 5th, 2014, 02:43 AM
#5
Re: Constructor with lots of parameters
sorry, i don know why.........
I was stuck
How do i create the Device Object with QScopedPointer as parameter, i mean it can't be like this: The scoped pointer is not copyable.
Device dev(QScopedPointer<Data>(new Data()), ....);
-
August 5th, 2014, 02:57 AM
#6
Re: Constructor with lots of parameters
Oh yes, I missed that. I would use a std::unique_ptr, but there seems to be no equivalent in Qt. Since there's no move constructor either, it looks like you have three choices:
- Use a QSharedPointer as the parameter. It would then make sense to also use a QSharedPointer as the member, but is undesirable if shared pointer semantics are undesirable.
- Pass the QScopedPointer by reference. Unfortunately, this means that the argument will have to be named.
- Pass an ordinary pointer. This is okay since you are going to take ownership immediately, but the caller must be clear on this.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|