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

    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;

  2. #2
    Join Date
    Jul 2002
    Posts
    788

    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?

  3. #3
    Join Date
    Apr 1999
    Posts
    27,449

    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

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

    Re: About returning QSharedPointer or raw pointer

    Quote 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.
    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
  •  





Click Here to Expand Forum to Full Width

Featured