Results 1 to 3 of 3

Thread: Inheritance and QExplicitlySharedDataPointer

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #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
  •  
Qt is a trademark of The Qt Company.