Results 1 to 5 of 5

Thread: A simple basic fundamental doubt !

  1. #1

    Default A simple basic fundamental doubt !

    Hi,

    I was just trying to understand the concept behind qobject_cast and have landed in to some dilemma . I would be more than happy if some one could tell me the difference between the two representation of the same logic. I am here by trying to expand the q_object macro to see how it is using the meta object for dynamic identification of the object type.

    Preconditions
    ----------------
    I have a QObject subclass,

    Qt Code:
    1. class QMyTestClass : public QObject
    2. {
    3. Q_OBJECT
    4. public:
    5. QMyTestClass(){}
    6. int Get(){return 23;}
    7. };
    8.  
    9. QObject *n ;
    10. QMyTestClass *o
    To copy to clipboard, switch view to plain text mode 

    First
    -----
    Qt Code:
    1. //QMyTestClass
    2. QMyTestClass* l = reinterpret_cast<QMyTestClass*>(0);
    3. const QMetaObject m = l->staticMetaObject;
    4. n = m.cast(myptr);
    5. o = static_cast<QMyTestClass*> (n);
    To copy to clipboard, switch view to plain text mode 
    Second
    ---------
    Qt Code:
    1. n = reinterpret_cast<QMyTestClass*>(0)->staticMetaObject.cast(myptr);
    2. o = static_cast<QMyTestClass*>(n);
    To copy to clipboard, switch view to plain text mode 

    In the first scenario I am getting the cast as NULL (i.e. o = NULL) where as in the second case I am getting the same as the correct cast. Just wondering what's the difference.

    Regards,
    Subhransu
    Last edited by wysota; 14th August 2008 at 06:55. Reason: missing [code] tags

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

    Default Re: A simple basic fundamental doubt !

    The first one won't work. The point of this statement is to have a pointer to a class without actual object of this class. It's a terrible (or a very nice, depending on the point of view) hack to make the template mechanism cast the result to a proper class.

  3. #3
    Join Date
    Dec 2006
    Posts
    849
    Thanks
    6
    Thanked 163 Times in 151 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: A simple basic fundamental doubt !

    First, staticMetaObject ist static, so just use QMyTestClass::staticMetaObject.
    Also, no need to reinterpret_cast a 0. If you just write QMyTestClass* l=0, it is just as well. (Better actually, because casts are bad, as is widely known...)

    Qt Code:
    1. n = QMyTestClass::staticMetaObject.cast(myptr); // better
    To copy to clipboard, switch view to plain text mode 

    Ok, to your issue:
    * what is myptr?
    * the cast function is basically an internal of qobject_cast. It returns 0 if the pointer is not of the needed QObject subtype. Your static_cast does nothing but adjust (after the typecheck) to that subtype.
    * The two examples should do the same.

    Please give us your complete example. (Are you sure myptr is the same before executing the two versions of that code?)

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

    Default Re: A simple basic fundamental doubt !

    Quote Originally Posted by caduel View Post
    First, staticMetaObject ist static, so just use QMyTestClass::staticMetaObject.
    What if you don't know the class name? That's the point of the whole statement.


    (Better actually, because casts are bad, as is widely known...)
    Hmm... why so? Bad casts are bad, good casts are good. qobject_cast<>() is among the latter.

    Qt Code:
    1. n = QMyTestClass::staticMetaObject.cast(myptr); // better
    To copy to clipboard, switch view to plain text mode 
    Try making a template function that performs a cast based on the type passed to it. Or implement a series of functions for each and every QObject subclass (existing and future).

    * The two examples should do the same.
    It's not that simple.

  5. #5
    Join Date
    Dec 2006
    Posts
    849
    Thanks
    6
    Thanked 163 Times in 151 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: A simple basic fundamental doubt !

    It's not that simple.
    Could you elaborate on that, pleaase?

Similar Threads

  1. very simple and basic FAM/Gamin wrapper for qt
    By momesana in forum Qt-based Software
    Replies: 0
    Last Post: 8th April 2008, 12:46
  2. Basic C++ doubt
    By munna in forum General Programming
    Replies: 8
    Last Post: 30th November 2006, 20:54

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.