-
May 27th, 2014, 01:53 AM
#1
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.
-
May 27th, 2014, 02:17 AM
#2
Re: reference to const
Originally Posted by mce
i want to store reference to a const object in my class as a member variable
Why?
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?
-
May 27th, 2014, 02:35 AM
#3
Re: reference to const
Originally Posted by laserlight
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.
-
May 27th, 2014, 02:53 AM
#4
Re: reference to const
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:
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.
-
May 27th, 2014, 03:05 AM
#5
Re: reference to const
Originally Posted by mce
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.
-
May 27th, 2014, 03:17 AM
#6
Re: reference to const
Originally Posted by zizz
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,
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 ).
-
May 30th, 2014, 03:20 PM
#7
Re: reference to const
Originally Posted by superbonzo
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.
-
May 30th, 2014, 04:19 PM
#8
Re: reference to const
Originally Posted by zizz
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|