-
July 30th, 2014, 02:37 AM
#1
pass by Reference to smart pointer?
hi
I just want to know if there is any real difference between the two below, if yes, when would i use one over the other? I would thought the "&" is pointless in below function, as far as the data is concerned.., the only things is with "&", if the pointer address value is changed in Test function, it will affect the caller's copy of data. Both function should behave the same if data is changed.
Code:
Between
void Test(QSharedPointer<Data> data)
{
}
and
void Test(QSharedPointer<Data> & data)
{
}
Last edited by mce; July 30th, 2014 at 02:41 AM.
-
July 30th, 2014, 07:45 AM
#2
Re: pass by Reference to smart pointer?
the first function defines a function that tabes a QSharedPointer object
the 'data' here is a COPY of the one that was passed in.
the 2nd takes a function that takes a QSharedPointer reference
the 'data' here is a reference to the one that was passed in.
the 2 functions are very different in nature. Both may be correct. It depends what you expect to be doing with data.
I would say that you probably wanted the 2nd version, but you probably want to make it a const reference.
-
July 30th, 2014, 09:35 AM
#3
Re: pass by Reference to smart pointer?
Originally Posted by mce
I just want to know if there is any real difference between the two below, if yes, when would i use one over the other?
Use the second one but make it const reference (as was suggested).
You pass by value only when it's sufficiently cheap. The rule of thumb is nothing bigger than a primitive, that is a double at the most. In this case you're passing a reference counting smart pointer (I suppose) and that would be two pointers (one to an object and one to a reference counter). In size that's equivalent to a double so here you could use pass by value BUT it also involves first an increment of the reference counter when the function is called and then a decrement when the function returns. This increases the cost tilting the equation over in favour of const reference.
So always pass a reference counting smart pointer by const reference. It's faster than by value and has a noticeble performance impact on OO programs which rely heavily on smart pointers.
Last edited by razzle; July 30th, 2014 at 03:14 PM.
-
July 30th, 2014, 09:24 PM
#4
Re: pass by Reference to smart pointer?
I have found a situation where i have to use QSharedPointer<Test>& without the "const" in its parameter., where in this particular case, the function pass a null parameter in... letting the callee to create the object with reference to it.
Code:
main()
{
......//
QDataStream in(&ff);
QMap<QString,QSharedPointer<Test>> p_map;
in >>p_map;
}
QDataStream &operator>>(QDataStream &stream, QSharedPointer<Test>& test)
{
//i am creating new Test here
test = QSharedPointer<Test>(new Test());
stream >> *test;
}
rather than
Code:
QDataStream &operator>>(QDataStream &stream, const QSharedPointer<Test>& test)
{
}
-
July 30th, 2014, 11:56 PM
#5
Re: pass by Reference to smart pointer?
Originally Posted by mce
I have found a situation where i have to use QSharedPointer<Test>& without the "const" in its parameter.
You're using the reference parameter to return a value so it cannot be const (it's an output (return) parameter). This also means passing this parameter by value isn't an option.
But in cases where you can pass by value use const reference instead (that is when the reference counting smart pointer is an input parameter).
Last edited by razzle; July 31st, 2014 at 01:58 AM.
-
July 31st, 2014, 07:12 AM
#6
Re: pass by Reference to smart pointer?
simple rules to follow
1) if you only want to pass a POD value or variable that won't allow the function to modify the variable that was passed in:
POD type: pass by value
anything else: pass by const reference
2) if you want to pass a variable and you want the function to modify the variable so the calling function will have a modified variable after the function call:
pass by reference
3) Use pass by (const) pointer (or smart pointer) only if
- you HAVE TO (and think this through several times) be able to pass in NULL.
- if for some other strange reason neither 1 or 2 will work for you (again, make absolutely sure).
Pointers make things 'complex' and more awkward to use for novice users, and it Always adds extra questions such as:
Who ownes the pointer ? do I need to delete the pointer ? if I do need to delete, is a delete ok or do I need to delete[] or some other form of deallocation ?
you can save yourself, and whomever will be using your classes or has to maintain your code a lot of headaches by simply not using pointers unless you really really have to.
it's 'ok' to use pointers in the implementation of your class (if you have to), but ideally you want your public interface to be without them.
Last edited by OReubens; July 31st, 2014 at 07:14 AM.
-
July 31st, 2014, 08:48 AM
#7
Re: pass by Reference to smart pointer?
Originally Posted by OReubens
simple rules to follow
My corresponding "simple rules" differ a lot. Here's my advice for the same cases:
1) pass by value or const reference
In principle they're functionally equivalent so it becomes a question of efficiency. Use pass by value when copying is cheap (nothing bigger than a primitive), otherwise prefer const reference.
A reference counting smart pointer usually has the size of a double so pass by value would be fine. BUT copying also involves a counter being incremented/decremented and this increases the cost tilting the balance over in favor of const reference. Always pass smart pointers by const reference to avoid this overhead.
2) pass by reference
I agree, but output (return) parameters should be avoided. If possible pass by value or const reference and let the function return a new value instead.
3) pass by pointer or smart pointer
Avoid raw pointers and use smart pointers instead, especially in public interfaces.
In OO it's very common to use smart pointers to represent objects throughout. The objects themselves are hidden from public view. There are two main reasons for this. It makes object life time management almost as easy as using a garbage collector, and it avoids the slicing problem associated with object copying.
Last edited by razzle; August 4th, 2014 at 05:42 AM.
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
|