Results 1 to 3 of 3

Thread: Inheritance and QExplicitlySharedDataPointer

  1. #1
    Join Date
    Jun 2009
    Posts
    5

    Default Inheritance and QExplicitlySharedDataPointer

    What is the best way to use QExplicitlySharedDataPointer and inherited classes. I would like when the BaseClass exits on it's own to have a my d pointer be QExplicitlySharedDataPointer <BaseClassPrivate> and when I have a Derived class on top of this base class I'd like to have d be a QExplicitlySharedDataPointer <DerivedClassPrivate>.

    I tried making DerivedClassPrivate inherit from BaseClassPrivate, and then make the d pointer protected and re-define the d-pointer in my derived class, but it seems now that I have two copies of the d-pointer both local to the class they are defined in... which is not what I want.

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Inheritance and QExplicitlySharedDataPointer

    There are actually two ways of doing this. What I usually do is that I don't use QExplicitlySharedDataPointer but instead I implement this functionality myself using QAtomic* classes. Another approach is to implement the QExplicitlySharedDataPointer::clone() for your data type. It makes it possible to decide about the type of the pimpl object based on the public component. Then you can declare the shared pointer in the base class and assign proper pimpls in subclasses. It still requires you to do the cast in subclasses when you wish to access members declared in the subclass.

  3. #3
    Join Date
    Mar 2010
    Posts
    3
    Qt products
    Qt4
    Platforms
    Windows

    Post Re: Inheritance and QExplicitlySharedDataPointer

    I also had same question, but my aim was to use standard QT classes like QSharedData and QExplicitlySharedDataPointer ... solution I came upon is (small sample):

    Qt Code:
    1. class BaseClass{
    2. public:
    3. BaseClass(){
    4. _d = new BaseData();
    5. }
    6.  
    7. protected:
    8. BaseData(BaseData *data){
    9. _d = data;
    10. }
    11.  
    12. QExplicitlySharedDataPointer<BaseData> _d;
    13. };
    14.  
    15. class DerivedClass : public BaseClass{
    16. public:
    17. DerivedClass() : BaseClass(new DerivedData()){
    18. _d = static_cast<DerivedData*>(BaseClass::_d.data());
    19. }
    20.  
    21. // Construtor for upcasting. This must be implemented if we want to do upcasting with variables placed in stack.
    22. DerivedClass(BaseClass &bs) : BaseClass(bs){
    23. _d = static_cast<DerivedData*>(BaseClass::_d.data());
    24. }
    25.  
    26. protected:
    27. QExplicitlySharedDataPointer<DerivedData> _d;
    28. };
    29.  
    30. main(){
    31. DerivedClass dc;
    32. BaseClass bs = dc;
    33. DerivedClass dc2 = bs;
    34. DerivedClass dc3 = static_cast<DerivedClass>(bs); // Same result as above
    35.  
    36. // After all these operations we have only single instance of shared data
    37. }
    To copy to clipboard, switch view to plain text mode 

    The result is that both base class and derived class has separate explicitly shared pointers named "_d". But its impossible to avoid this!!! The best you can achieve here is variable overloading. But this pointer points to the same data class instance!!! If somebody will find better solution, it would be interesting to hear it.

    P.S. after posting this I just thought that it is also possible not to define variable "_d" in derived class but use BaseClass variable instead. Just in this case well have to do more casting operations when accesing values.
    Last edited by Algirdasss; 28th May 2010 at 09:19.

Similar Threads

  1. QExplicitlySharedDataPointer
    By qtuser20 in forum Qt Programming
    Replies: 1
    Last Post: 15th September 2009, 07:05
  2. inheritance
    By steiner in forum Qt Programming
    Replies: 4
    Last Post: 30th October 2007, 20:17
  3. inheritance
    By mickey in forum General Programming
    Replies: 11
    Last Post: 28th September 2007, 21:54
  4. How much inheritance do you use?
    By Michiel in forum General Programming
    Replies: 8
    Last Post: 1st August 2006, 22:29
  5. QSA and inheritance
    By jwintz in forum Qt Programming
    Replies: 1
    Last Post: 13th June 2006, 14:05

Tags for this Thread

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.