Constructor with lots of parameters
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6

Thread: Constructor with lots of parameters

  1. #1
    Join Date
    Jul 2002
    Posts
    772

    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(.................................................)
    }

  2. #2
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,289

    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.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  3. #3
    Join Date
    Jul 2002
    Posts
    772

    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())
    {

    }

  4. #4
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,289

    Re: Constructor with lots of parameters

    Quote 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.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  5. #5
    Join Date
    Jul 2002
    Posts
    772

    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()), ....);

  6. #6
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,289

    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.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center