-
May 29th, 2014, 04:12 AM
#1
About returning QSharedPointer or raw pointer
Hi
I have a class which looks like the following:
Code:
class Gadgets
{
public:
Data* GetData() const
{
return _data.data();
}
const Data * GetDataConst() const
{
return _data.data();
}
private:
QSharedPointer<Data> _data;
}
You may ask why i have the second GetData() method that return a pointer to my private member, which will then expose my private member externally. I provide this function just for the case where user can do the following
Code:
Gadgets * myGadgets = new Gadgets();
myGadgets->GetData()->ModifyData(.....);
My question pertains to the use of QSharedPointer of data(), where in the doc, it says
T * QSharedPointer: ata () const
Returns the value of the pointer referenced by this object.
Note: do not delete the pointer returned by this function or pass it to another function that could delete it, including creating QSharedPointer or QWeakPointer objects.
Whereas, if i were to use
Code:
Data* data as my private member class instead of QSharedPointer;
I will not need to worry if the caller do as follow
Code:
Data * data = new Data();
Data *data2 = myGadgets.GetData();
data2 = data;
delete data;
-
May 29th, 2014, 04:22 AM
#2
Re: About returning QSharedPointer or raw pointer
I believe nothing to worry too if i were to use QSharedPointer<Data> as it is also a copy of the pointer being returned from function GetData() after i do
_data.data();
what the user deleted is just the copy of the above pointer. Right?
-
May 29th, 2014, 04:25 AM
#3
Re: About returning QSharedPointer or raw pointer
1) I don't know what your question is. You mention QSharedPointer (I guess it is similar to std::shared_ptr) and a raw pointer, but no real question.
2)
I will not need to worry if the caller do as follow
You can't stop the user from doing stupid things. If the caller wants to shoot themselves in the foot, you can't stop them. C++ is a language where it is impossible to predict what a user of the class will do. All you can do is document the known pitfalls, and hope that the user reads the documentation and is a competent programmer.
Regards,
Paul McKenzie
-
May 29th, 2014, 04:25 AM
#4
Re: About returning QSharedPointer or raw pointer
Originally Posted by mce
My question pertains to the use of QSharedPointer of data(), where in the doc, it says
(...)
Whereas, if i were to use
Code:
Data* data as my private member class instead of QSharedPointer;
I will not need to worry if the caller do as follow
Code:
Data * data = new Data();
Data *data2 = myGadgets.GetData();
data2 = data;
delete data;
You don't need to worry about that either way: data2 first points to the same object that your member pointer points to, and then it is immediately overwritten such that it points to the same object that the local variable named data points to, after which the object that both local pointers pointed to is destroyed. The member pointer and the object that it points to are both completely unaffected.
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
|