Results 1 to 4 of 4

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

Threaded View

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

    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

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.