CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    Jul 2002
    Posts
    788

    reference to const

    Hi,

    i want to store reference to a const object in my class as a member variable, as follow:

    I basically want a readonly reference to |Data| in Device object.


    Code:
    class Device
    {
    Device(const QList<QSharedPointer<Data>> & dataList) : _listRef(dataList)
    {
    
    }
    protected:
    const QList<QSharedPointer<Data>> & _listRef;
    }
    This does not allow me to initialize _listRef as something like NULL when it is not applicable.
    Also, i must change all my constructors and its child class to include an initialization of _listRef!!


    What is the alternative? Is pointer the nearest? which of the following should be used?

    Code:
    const QList<QSharedPointer<Data>> * _listRef;
    
    or
    
    const QList<QSharedPointer<Data>> *const  _listRef;
    
    or 
    
    const QSharedPointer<QList<QSharedPointer<Data>>>   _listRef; ????
    Last edited by mce; May 27th, 2014 at 02:03 AM.

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

    Re: reference to const

    Quote Originally Posted by mce
    i want to store reference to a const object in my class as a member variable
    Why?

    Quote Originally Posted by mce
    What is the alternative? Is pointer the nearest? which of the following should be used?
    It depends on what you are trying to do. Why does a Device object need a reference/pointer/whatever to a QList object? When are you going to associate this QList object with the Device object?
    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
    788

    Re: reference to const

    Quote Originally Posted by laserlight View Post
    Why?




    It depends on what you are trying to do. Why does a Device object need a reference/pointer/whatever to a QList object? When are you going to associate this QList object with the Device object?
    Well as i mentioned, i want a "readonly" reference to all the Data inside a list.

    The List of data is passed to the device during device initialization. Inside device, i may check every now and then the contents of Data (which is updated regularly) in order to do calculations.

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

    Re: reference to const

    Quote Originally Posted by mce
    The List of data is passed to the device during device initialization. Inside device, i may check every now and then the contents of Data (which is updated regularly) in order to do calculations.
    If the Device object will never be associated with a different QList object, and if you do not need to use copy assignment with Device objects, then a const reference member sounds fine. However, I noticed:
    Quote Originally Posted by mce
    This does not allow me to initialize _listRef as something like NULL when it is not applicable.
    This sounds like you might pass the list of data to the device at some other time, or is it the case that a device might not have a list of data? If so, a pointer is fine. It can be a normal pointer, or if you prefer to use a smart pointer, it can be a weak pointer.
    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
    Aug 2013
    Posts
    55

    Re: reference to const

    Quote Originally Posted by mce View Post
    This does not allow me to initialize _listRef as something like NULL when it is not applicable.
    You could add a default constructor where _listRef is initialized with an empty QList like,

    Code:
    class Device {
    public:
        Device() : _listRef(QList<QSharedPointer<Data>>()) {}
        explicit Device(const QList<QSharedPointer<Data>> & dataList) : _listRef(dataList) {}
    protected:
        const QList<QSharedPointer<Data>> & _listRef;
    };
    A safer solution is to treat the QList by value or even better to use a QSharedPointer also for the QList (and not just for Data).
    Last edited by zizz; May 27th, 2014 at 03:19 AM.

  6. #6
    Join Date
    Oct 2008
    Posts
    1,456

    Re: reference to const

    Quote Originally Posted by zizz View Post
    You could add a default constructor where _listRef is initialized with an empty QList like,

    Code:
    class Device {
    public:
        Device() : _listRef(QList<QSharedPointer<Data>>()) {}
    [...]
    not a good idea, you're creating a dangling reference. Anyway,

    Quote Originally Posted by mce
    This does not allow me to initialize _listRef as something like NULL when it is not applicable.
    define "not applicable"; note that making the reference/pointer "nullable" increases code complexity ( you need to check that the pointer is not null and act accordingly almost every time is used ) and it buys you nothing unless <having no list> has a specific meaning. IOW, if Device can live with an empty list of Data then using such empty list as zizz suggested is the way to go ( avoiding the dangling reference issue, of course; eg, the Device ctor caller should have the responsability of passing an empty list, not the Device itself ).

  7. #7
    Join Date
    Aug 2013
    Posts
    55

    Re: reference to const

    Quote Originally Posted by superbonzo View Post
    not a good idea, you're creating a dangling reference. Anyway,
    How is that? What's dangling? _listRef is initialized to an empty QList or am I mistaken?
    Last edited by zizz; May 30th, 2014 at 03:23 PM.

  8. #8
    Join Date
    Oct 2008
    Posts
    1,456

    Re: reference to const

    Quote Originally Posted by zizz View Post
    How is that? What's dangling? _listRef is initialized to an empty QList or am I mistaken?
    the reference is initialized with a qlist whose lifetime will not survive the Device ctor body ( or an exception thereof). Note that the usual lifetime extension of temporaries when bound to const& or && references does not apply in this case ....

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured