Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [Solved] Returning const QSharedPointer
QtWS25 Last Chance

[Solved] Returning const QSharedPointer

Scheduled Pinned Locked Moved General and Desktop
qsharedpointersmart pointers
4 Posts 2 Posters 3.5k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Joel BodenmannJ Offline
    Joel BodenmannJ Offline
    Joel Bodenmann
    wrote on last edited by Joel Bodenmann
    #1

    Let's consider the following code:

    class Foo {
    	int a;	
    };
    
    class Bar {
    public:
    	Bar() {
    		_foo = new Foo;
    	}
    	
    	const Foo* foo() const {
    		return _foo;
    	}
    
    private:
    	Foo* _foo;
    }
    

    How would I implement the same behavior using QSharedPointer? I understand how to use QSharedPointer but I don't understand how I would return a QSharedPointer that is a const pointer (the return type of the public method in the Bar class) so that the caller of Bar::foo() gets just "const access" to the Foo object.

    Industrial process automation software: https://simulton.com
    Embedded Graphics & GUI library: https://ugfx.io

    1 Reply Last reply
    0
    • D Offline
      D Offline
      DanWatkins
      wrote on last edited by
      #2
      This post is deleted!
      1 Reply Last reply
      0
      • D Offline
        D Offline
        DanWatkins
        wrote on last edited by DanWatkins
        #3

        Here is a complete example. The trick is to use QSharedPointer<const Foo>.

        #include <QSharedPointer>
        #include <QDebug>
        
        struct Foo
        {
            ~Foo()
            {
                qDebug() << "Foo destroyed";
            }
        
            int count() { return 7; }
            int sum(int a, int b) const { return a + b; }
        };
        
        
        class Bar
        {
        public:
            Bar() : mFoo(new Foo) {}
            QSharedPointer<Foo> foo() { return mFoo; }
            QSharedPointer<const Foo> constFoo() const { return mFoo; }
        
        private:
            QSharedPointer<Foo> mFoo;
        };
        
        
        int main()
        {
            //intentially store it as a local variable
            //this shows how the reference count is incremented
            QSharedPointer<const Foo> constFoo(nullptr);
            {
                {
                    Bar bar;
                    constFoo = bar.constFoo();
        
                    //error, discards const qualifiers of const Foo
                    //qDebug() << constFoo->count();
                    qDebug() << constFoo->sum(5, 6);
                    qDebug() << bar.foo()->count();
                }
        
                qDebug() << "bar has gone out of scope, constFoo holds the last reference";
            }
        }
        Joel BodenmannJ 1 Reply Last reply
        2
        • D DanWatkins

          Here is a complete example. The trick is to use QSharedPointer<const Foo>.

          #include <QSharedPointer>
          #include <QDebug>
          
          struct Foo
          {
              ~Foo()
              {
                  qDebug() << "Foo destroyed";
              }
          
              int count() { return 7; }
              int sum(int a, int b) const { return a + b; }
          };
          
          
          class Bar
          {
          public:
              Bar() : mFoo(new Foo) {}
              QSharedPointer<Foo> foo() { return mFoo; }
              QSharedPointer<const Foo> constFoo() const { return mFoo; }
          
          private:
              QSharedPointer<Foo> mFoo;
          };
          
          
          int main()
          {
              //intentially store it as a local variable
              //this shows how the reference count is incremented
              QSharedPointer<const Foo> constFoo(nullptr);
              {
                  {
                      Bar bar;
                      constFoo = bar.constFoo();
          
                      //error, discards const qualifiers of const Foo
                      //qDebug() << constFoo->count();
                      qDebug() << constFoo->sum(5, 6);
                      qDebug() << bar.foo()->count();
                  }
          
                  qDebug() << "bar has gone out of scope, constFoo holds the last reference";
              }
          }
          Joel BodenmannJ Offline
          Joel BodenmannJ Offline
          Joel Bodenmann
          wrote on last edited by
          #4

          Awesome, thank you very much!

          Industrial process automation software: https://simulton.com
          Embedded Graphics & GUI library: https://ugfx.io

          1 Reply Last reply
          0

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved