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. How to cast QSharedDataPointer
Qt 6.11 is out! See what's new in the release blog

How to cast QSharedDataPointer

Scheduled Pinned Locked Moved General and Desktop
4 Posts 3 Posters 1.7k Views 1 Watching
  • 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.
  • T Offline
    T Offline
    Tom C
    wrote on last edited by
    #1

    Hi !

    How to dynamic_cast a QSharedDataPointer<Base> object into a QSharedDataPointer<Derived> object whitout loosing the reference counting from the Base object ?

    NB: Base inherits QSharedData.
    NB: Derived inherits Base.

    1 Reply Last reply
    0
    • B Offline
      B Offline
      bjanuario
      wrote on last edited by
      #2

      Can u have a look at "QSharedDataPointer sample":http://cdumez.blogspot.pt/2011/03/implicit-explicit-data-sharing-with-qt.html

      Hope this helps ;)

      1 Reply Last reply
      0
      • W Offline
        W Offline
        wibstr
        wrote on last edited by
        #3

        [quote author="bjanuario" date="1370715462"]Can u have a look at "QSharedDataPointer sample":http://cdumez.blogspot.pt/2011/03/implicit-explicit-data-sharing-with-qt.html

        Hope this helps ;)[/quote]

        It doesn't. There's nothing in that article about polymorphism or dynamic casting.

        I'm also interested in this question. Does anyone have a good solution?

        1 Reply Last reply
        0
        • W Offline
          W Offline
          wibstr
          wrote on last edited by
          #4

          I've thought about this a bit and think I may have a solution. The idea is that only the base class contains a QSharedDataPointer and the derived classes access it via static_cast. It's essentially a form of static polymorphism. Here's the code:

          @
          class BaseDataType
          {
          const char* getter() const;
          void setter(const char*);
          };

          class DerivedDataType
          {
          const char* getter_derived() const;
          void setter_derived(const char*);
          };

          class BaseData : public QSharedData
          {
          public:
          // the standard ctor and copy ctor go here

          // my data is a pointer to a polymorphic type
          BaseDataType* ptr;
          };

          class Base
          {
          public:
          // the standard ctor and copy ctor go here

          protected:
          // subclasses use this to access the shared ptr and then static cast it
          QSharedDataPointer<BaseData> real_d;
          };

          class Derived
          {
          public:
          // ctor and copy ctor go here

          const char* getter_derived() const
          {
          // const access - won't cause a detach
          return d()->getter_derived();
          }

          void setter_derived(const char* str)
          {
          // non-const access - may cause a detach
          d()->setter_derived(str);
          }

          private:
          // these accessors replace the standard "d" pointer:

          // the -> won't cause a detach here
          const DerivedDataType* d() const {return static_cast<DerivedDataType*>(real_d->ptr);

          // the -> may cause a detach here
          DerivedDataType* d() {return static_cast<DerivedDataType*>(real_d->ptr);}

          };
          @

          This only just occured to me, so somebody please let me know if I'm missing something.

          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