Results 1 to 4 of 4

Thread: Using qSharedPointerConstCast syntax

  1. #1
    Join Date
    Jun 2016
    Posts
    3
    Thanks
    5
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default Using qSharedPointerConstCast syntax

    I have a function,

    const QVector<double> & getmData() {return mData;}

    I need to pass the return of this function to another function:

    processData(QSharedPointer<QVector<double> > data)

    I see that there is a function qSharedPointerConstCast(const QSharedPointer<T> &other) that should help me get from const QVector<double> & to QSharedPointer<QVector<double> > , but damned if I can make it work!! Something like...


    QSharedPointer<QVector<double> > rawData = qSharedPointerConstCast<QVector<double> >(getmData());

    but of course this throws a compile error, " no matching function for call to 'qSharedPointerConstCast(const QVector<double>&)'
    QSharedPointer<QVector<double> > rawData = qSharedPointerConstCast<QVector<double> >(getmData());"


    QSharedPointer<QVector<double> > rawData = qSharedPointerConstCast<QSharedPointer<QVector<dou ble> >>(iter->second->getmData());
    gives a no matching function error

    ^
    Last edited by scoutycat; 22nd June 2016 at 21:12.

  2. #2
    Join Date
    Dec 2009
    Location
    New Orleans, Louisiana
    Posts
    791
    Thanks
    13
    Thanked 153 Times in 150 Posts
    Qt products
    Qt5
    Platforms
    MacOS X

    Default Re: Using qSharedPointerConstCast syntax

    There may be a more desirable way to accomplish this, but here's *one* way... First, a couple of typedefs to improve readability IMHO:

    Qt Code:
    1. typedef QVector<double> DoubleVector;
    2. typedef QSharedPointer<DoubleVector> SharedDoubleVectorPtr;
    To copy to clipboard, switch view to plain text mode 

    Then the following code:

    Qt Code:
    1. DoubleVector dv = getmData();
    2. SharedDoubleVectorPtr sp(&dv);
    To copy to clipboard, switch view to plain text mode 

    Hope that helps.
    I write the best type of code possible, code that I want to write, not code that someone tells me to write!

  3. The following user says thank you to jefftee for this useful post:

    scoutycat (23rd June 2016)

  4. #3
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Using qSharedPointerConstCast syntax

    Quote Originally Posted by scoutycat View Post
    I see that there is a function qSharedPointerConstCast(const QSharedPointer<T> &other) that should help me get from const QVector<double> & to QSharedPointer<QVector<double> >
    How would that help you?

    You don't have a "const QSharedPointer<T>&".
    You don't even have a QSharedPointer.

    As jefftee wrote, you need to create a shared pointer.
    But jefftee's suggestion would make the shared pointer delete the vector it is pointing to but it hasn't been allocated on the heap.

    So you either need to create a copy on the heap or pass a custom delete function that doesn't actually delete the pointer.

    Cheers,
    _
    Last edited by anda_skoa; 23rd June 2016 at 09:20.

  5. The following user says thank you to anda_skoa for this useful post:

    scoutycat (23rd June 2016)

  6. #4
    Join Date
    Dec 2009
    Location
    New Orleans, Louisiana
    Posts
    791
    Thanks
    13
    Thanked 153 Times in 150 Posts
    Qt products
    Qt5
    Platforms
    MacOS X

    Default Re: Using qSharedPointerConstCast syntax

    Quote Originally Posted by anda_skoa View Post
    But jefftee's suggestion would make the shared pointer delete the vector it is pointing to but it hasn't been allocated on the heap.
    @anda_skoa, agreed, my focus was on getting him past his syntax error and with the minimal code the OP provided, I made some (perhaps incorrect) assumptions about his actual code.
    I write the best type of code possible, code that I want to write, not code that someone tells me to write!

  7. The following user says thank you to jefftee for this useful post:

    scoutycat (23rd June 2016)

Similar Threads

  1. Replies: 5
    Last Post: 28th October 2015, 21:10
  2. .pro file syntax
    By lxman in forum Qt Tools
    Replies: 4
    Last Post: 11th February 2011, 23:21
  3. Syntax confusion
    By baluk in forum Newbie
    Replies: 3
    Last Post: 11th December 2010, 15:58
  4. Regarding syntax
    By Yayati.Ekbote in forum Qt Programming
    Replies: 3
    Last Post: 27th January 2010, 14:15
  5. Odd Syntax
    By acxdotfm in forum Qt Programming
    Replies: 2
    Last Post: 24th October 2008, 20:23

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.