Results 1 to 4 of 4

Thread: Pimpl implemenation - compilation error on static_cast base* to Derieved*

  1. #1
    Join Date
    Jan 2006
    Location
    Kerala
    Posts
    371
    Thanks
    76
    Thanked 37 Times in 32 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Pimpl implemenation - compilation error on static_cast base* to Derieved*

    Hi,

    I was trying out the pimpl idiom and its implementation in Qt.
    I was wondering why the following code gives me an error in the data_pointer_test function. the error happens in both the static and the dynamic cast.

    AFAIK, static cast should be able perform a downcast. as the following code does not give me any error
    Qt Code:
    1. PrivateData *d = new QstObjectPrivate(0);
    2. QstObjectPrivate *back = static_cast<QstObjectPrivate *>( d);
    To copy to clipboard, switch view to plain text mode 
    Am I getting the basics wrong ?
    Here is the Pimpl implementation code ...
    Qt Code:
    1. p, li { white-space: pre-wrap; } // --- Private Data ---
    2. class PublicInterface;
    3. class PrivateData {
    4. friend class PublicInterface;
    5. public:
    6. PrivateData( PublicInterface *iface )
    7. :_interface(iface){}
    8. virtual ~PrivateData(){
    9. }
    10. // ---- data ----
    11. PublicInterface *_interface;
    12. };
    13. // --- Public Interface ---
    14. class PublicInterface{
    15. friend class PrivateData;
    16. protected:
    17. inline PublicInterface( PrivateData *d ):_data( d ){
    18. }
    19.  
    20. inline ~PublicInterface(){
    21. delete _data;
    22. }
    23. // ---- data ----
    24. PrivateData *_data;
    25. };
    26. // --- My Private Test Harness ---
    27. class QstObjectPrivate;
    28. class QstObject : public PublicInterface{
    29. public:
    30. QstObject();
    31. private:
    32. QstObjectPrivate* data_pointer_test() {
    33. return static_cast<QstObjectPrivate*>( _data );
    34. }
    35. };
    36. class QstObjectPrivate : public PrivateData{
    37. public:
    38. QstObjectPrivate( PublicInterface *iface ):PrivateData( iface ){
    39. }
    40. };
    41. QstObject::QstObject()
    42. :PublicInterface( new QstObjectPrivate(this) ){
    43. }
    44. int main(){
    45. QstObject test;
    46. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by sunil.thaha; 13th September 2007 at 09:54. Reason: reformatted to look better
    We can't solve problems by using the same kind of thinking we used when we created them

  2. #2
    Join Date
    Jan 2006
    Location
    Kerala
    Posts
    371
    Thanks
    76
    Thanked 37 Times in 32 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Pimpl implemenation - compilation error on static_cast base* to Derieved*

    Is this very simple that no one want to answer ? or
    We can't solve problems by using the same kind of thinking we used when we created them

  3. #3
    Join Date
    Oct 2006
    Posts
    279
    Thanks
    6
    Thanked 40 Times in 39 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Pimpl implemenation - compilation error on static_cast base* to Derieved*

    You can't define the function there, since at that point the compiler doesn't know that QstObjectPrivate inherits PrivateData you have to implement it at the end of the file:
    Qt Code:
    1. ...
    2. class QstObject : public PublicInterface
    3. {
    4. public:
    5. QstObject();
    6. private:
    7. inline QstObjectPrivate* data_pointer_test();
    8. };
    9.  
    10. class QstObjectPrivate : public PrivateData
    11. {
    12. public:
    13. QstObjectPrivate( PublicInterface *iface )
    14. : PrivateData( iface )
    15. {
    16. }
    17. };
    18.  
    19. QstObject::QstObject()
    20. : PublicInterface( new QstObjectPrivate(this) )
    21. {
    22. }
    23.  
    24. QstObjectPrivate* QstObject::data_pointer_test()
    25. {
    26. return static_cast<QstObjectPrivate*>( _data );
    27. }
    28. ...
    To copy to clipboard, switch view to plain text mode 

  4. #4
    Join Date
    Jan 2006
    Location
    Kerala
    Posts
    371
    Thanks
    76
    Thanked 37 Times in 32 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Pimpl implemenation - compilation error on static_cast base* to Derieved*

    Thanks a lot !!
    and that helped me make sense of why Qt's pimpl implementation uses reinterpret cast
    We can't solve problems by using the same kind of thinking we used when we created them

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.